- Notifications
You must be signed in to change notification settings - Fork0
Demo repository for habr.com article about faster Go string concatenation.
License
NotificationsYou must be signed in to change notification settings
quasilyte/concat
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Demo package forУскорение конкатенации строк в Go своими руками article.
This package provides simple functions that return concatenation results.Can work faster than Go+
operator.
You should not use this package, really. It's just an example.
BenchmarkConcat2Operator/short-8 20000000 84.4 ns/opBenchmarkConcat2Operator/longer-8 10000000 158 ns/opBenchmarkConcat2Builder/short-8 20000000 70.7 ns/opBenchmarkConcat2Builder/longer-8 10000000 127 ns/opBenchmarkConcat2/short-8 30000000 57.3 ns/opBenchmarkConcat2/longer-8 20000000 106 ns/opBenchmarkConcat3Operator/short-8 20000000 103 ns/opBenchmarkConcat3Operator/longer-8 10000000 217 ns/opBenchmarkConcat3Builder/short-8 20000000 89.9 ns/opBenchmarkConcat3Builder/longer-8 5000000 249 ns/opBenchmarkConcat3/short-8 20000000 85.0 ns/opBenchmarkConcat3/longer-8 10000000 189 ns/op
Number one is unsafe concatenation, second isstrings.Builder
with preallocatedbuffer and "obvious" concatenation is the slowest one... unlessCL123256 is applied.
Using thebenchstat
, here is the difference betweenconcat
and+
:
name old time/op new time/op deltaConcat2/short-8 84.4ns ± 2% 64.3ns ± 4% -23.85% (p=0.000 n=14+15)Concat2/longer-8 138ns ± 1% 118ns ± 1% -14.83% (p=0.000 n=13+15)Concat3/short-8 105ns ± 5% 82ns ± 5% -22.29% (p=0.000 n=15+14)Concat3/longer-8 218ns ± 1% 192ns ± 1% -11.95% (p=0.000 n=15+15)
If compared with AMD64 asm version for concat2:
name old time/op new time/op deltaConcat2/short-8 84.4ns ± 0% 56.9ns ± 5% -32.54% (p=0.000 n=15+15)Concat2/longer-8 138ns ± 1% 107ns ± 0% -22.51% (p=0.000 n=13+15)
As a bonus, asm version also makes empty strings concatenation optimization,just like runtime version of concat would.
package mainimport ("fmt""github.com/Quasilyte/concat")funcmain() {v:="world!"fmt.Println(concat.Strings("hello, ",v))// => "hello, world!"}