I have seen on this website a question about the speed of string concatenation. In that topic people wrote about some ephemeral benchmarks with strange numbers. How to efficiently concatenate strings in Go?
I have decided to check those speeds and wrote a test. My test shows other results. On big sizes "+" operator is faster than other methods. Is that right?
Here is my code.
package main
import (
"bytes"
"fmt"
"runtime/debug"
"time"
)
const variations = 30
var time1, time2 time.Time
var delta, catcher string
var x, deltaSize, k, dataSize, operations uint64
var i, j, x_min uint64
var l int
var delta_byte []byte
var method1Speed, method2Speed, method3Speed, method3ASpeed, method2ASpeed [variations]uint64
var dataTotal [variations]uint64
var tmp []byte
func main() {
x_min = 2
operations = 1
for x = variations; x >= x_min; x = x - 2 {
deltaSize = 1 << x // 2^x
dataSize = operations * deltaSize
dataTotal[x-1] = dataSize
fmt.Println("Step #", x, "delta=", deltaSize, "op.=", operations, "data=", dataSize)
fmt.Println("Preparing Data...")
delta_byte = make([]byte, deltaSize)
for i = 0; i < deltaSize; i++ {
delta_byte[i] = 255
}
delta = string(delta_byte)
delta_byte = nil
catcher = ""
debug.FreeOSMemory()
fmt.Println("Testing Method #1...")
time1 = time.Now()
for j = 1; j <= operations; j++ {
//----------------------------
catcher += delta
//----------------------------
}
time2 = time.Now()
method1Speed[x-1] = uint64((1000000 * float64(dataSize)) / float64(time2.Sub(time1).Nanoseconds())) // KiB/sec.
catcher = ""
debug.FreeOSMemory()
fmt.Println("Testing Method #2...")
time1 = time.Now()
for j = 1; j <= operations; j++ {
//----------------------------
stringsJoinViaCopy(&catcher, &catcher, &delta)
//----------------------------
}
time2 = time.Now()
method2Speed[x-1] = uint64((1000000 * float64(dataSize)) / float64(time2.Sub(time1).Nanoseconds())) // KiB/sec.
catcher = ""
debug.FreeOSMemory()
fmt.Println("Testing Method #3...")
time1 = time.Now()
for j = 1; j <= operations; j++ {
//----------------------------
stringsJoinViaBuffer(&catcher, &catcher, &delta)
//----------------------------
}
time2 = time.Now()
method3Speed[x-1] = uint64((1000000 * float64(dataSize)) / float64(time2.Sub(time1).Nanoseconds())) // KiB/sec.
catcher = ""
debug.FreeOSMemory()
fmt.Println("Testing Method #3A...")
time1 = time.Now()
buffer := bytes.NewBuffer(nil)
for j = 1; j <= operations; j++ {
//----------------------------
buffer.WriteString(delta)
//----------------------------
}
catcher = buffer.String()
time2 = time.Now()
method3ASpeed[x-1] = uint64((1000000 * float64(dataSize)) / float64(time2.Sub(time1).Nanoseconds())) // KiB/sec.
catcher = ""
debug.FreeOSMemory()
fmt.Println("Testing Method #2A...")
time1 = time.Now()
tmp = make([]byte, int(operations)*len(delta)) // Cheating (guessing) with size
l = 0
for j = 1; j <= operations; j++ {
//----------------------------
l += copy(tmp[l:], delta)
//----------------------------
}
catcher = string(tmp)
time2 = time.Now()
method2ASpeed[x-1] = uint64((1000000 * float64(dataSize)) / float64(time2.Sub(time1).Nanoseconds())) // KiB/sec.
catcher = ""
delta = ""
debug.FreeOSMemory()
///
operations *= 2
}
// Show Results
fmt.Println("#. ops. Total Data, B. Speed (KiB/sec) M1 M2 M3 M3A M2A")
for x = x_min; x <= variations; x = x + 2 {
dataSize = 1 << x // 2^x
operations = 1 << (variations - x)
fmt.Println(x, operations, dataTotal[x-1], method1Speed[x-1], method2Speed[x-1], method3Speed[x-1],
method3ASpeed[x-1], method2ASpeed[x-1])
}
}
//------------------------------------------------------------------------------
func stringsJoinViaBuffer(dest, a, b *string) {
// Joins two strings (a & b) using Buffer and puts them into dest.
buffer := bytes.NewBuffer(nil)
buffer.WriteString(*a)
buffer.WriteString(*b)
*dest = buffer.String()
}
//------------------------------------------------------------------------------
func stringsJoinViaCopy(dest, a, b *string) {
x := make([]byte, len(*a)+len(*b))
i := 0
i += copy(x[i:], *a)
i += copy(x[i:], *b)
*dest = string(x)
}
Here are results
#. ops. Total Data, B. Speed (KiB/sec) M1 M2 M3 M3A M2A
2 268435456 65536 236 109 57 108413 301653
4 67108864 131072 464 227 113 251519 576660
6 16777216 262144 895 410 202 225300 626165
8 4194304 524288 1514 672 351 205068 552088
10 1048576 1048576 3187 1412 756 207588 532239
12 262144 2097152 7980 3238 1727 209447 592230
14 65536 4194304 16361 6553 3641 230521 536320
16 16384 8388608 29568 12170 6835 241752 604050
18 4096 16777216 55158 23950 13549 238039 563997
20 1024 33554432 98348 43400 25958 216947 521189
22 256 67108864 168906 80442 48725 231806 534722
24 64 134217728 299127 129035 89686 254403 519534
26 16 268435456 529730 207405 153894 284578 506730
28 4 536870912 1167316 353510 268546 359990 523471
30 1 1073741824 909950698305 503703 581848 572763 579852
Seems like it works when you either have a lot of data going constantly or can cheat with size guessing... Is it correct? If there are ocassional strings, simple "+" is better? Somehow, in the mentioned question people measured byte transfers without real-world tasks.
Somehow in the step #26 "+" operator is faster even than cheating with size guessing!