[go-nuts] Re: Best way to truncate a ridiculously long string in GO

2017-08-22 Thread Val
Thanks Peter, this is very interesting. Indeed I would not have anticipated (though it makes some sense) that calling append would outperform the manual loop, when the prefix is big, despite the overhead of a builtin func call. On Tuesday, August 22, 2017 at 7:22:42 PM UTC+2, peterGo wrote: >

[go-nuts] Re: Best way to truncate a ridiculously long string in GO

2017-08-22 Thread peterGo
Val, That's a lot of speculation! The original benchmark applies to the original question: prefix := verylongstring[:3] If we change the parameters of the benchmark then we expect to get different results. For example, read the Go gc compiler code. There's a stack/heap optimization for 32

[go-nuts] Re: Best way to truncate a ridiculously long string in GO

2017-08-22 Thread Val
FWIW, append is most often a small performance penalty when the number of elements is known ahead. And for some reason, using built-in func copy, or an explicit loop, is slightly faster on my workstation than BenchmarkConvert. Also, "small benchmarks are hard" so I suspect the small allocations i

[go-nuts] Re: Best way to truncate a ridiculously long string in GO

2017-08-22 Thread peterGo
A benchmark; https://play.golang.org/p/oUyeldDG5Q $ cat strslice_test.go package main import ( "strings" "testing" ) var ( s = strings.Repeat("a very, very long string", 4096) prefix string ) func BenchmarkNil(b *testing.B) { for i := 0; i < b.N; i++ { prefix =

[go-nuts] Re: Best way to truncate a ridiculously long string in GO

2017-08-21 Thread djadala
https://play.golang.org/p/YXXlJlsNGa Hi, i'm not sure if compiler is smart enough to optimize v1, but likely v2 ends in separate memory. On Tuesday, August 22, 2017 at 3:04:42 AM UTC+3, Travis Keep wrote: > > Suppose you have code like this > > > // verylongstring may be several thousand b