Skip to content

Instantly share code, notes, and snippets.

@sgryjp
Last active September 1, 2017 00:41
Show Gist options
  • Save sgryjp/775c007e92518ed6fb695729e8c79bec to your computer and use it in GitHub Desktop.
Save sgryjp/775c007e92518ed6fb695729e8c79bec to your computer and use it in GitHub Desktop.
Golang: Performance test of string variable copy
package main
import "testing"
import "strings"
import "fmt"
type StructWithString struct {
Str string
}
func Benchmark_StringCopy(b *testing.B) {
strLen := new(int)
scanTest := func(b *testing.B) {
a := strings.Repeat("a", *strLen)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = strings.Index(a, "!")
}
}
copyStringTest := func(b *testing.B) {
x := strings.Repeat("a", *strLen)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = x
}
}
copyStructTest := func(b *testing.B) {
x := StructWithString{strings.Repeat("a", *strLen)}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = x
}
}
for i := 1; i < 10; i++ {
*strLen = i * 1024 * 1024
b.Run(fmt.Sprintf("%dMB_ScanTest", i), scanTest)
b.Run(fmt.Sprintf("%dMB_CopyString", i), copyStringTest)
b.Run(fmt.Sprintf("%dMB_CopyStruct", i), copyStructTest)
}
}
@sgryjp
Copy link
Author

sgryjp commented Sep 1, 2017

It seems Go does not copy content (characters inside) of string variables when making a copy of it -- copying is far faster than scanning it (Go should copying the address of the string content, not the content them selves). So, we don't need to worry about performance of copying string variables.

Test result on my laptop:

suguru@x1 ~/go/src/github.com/sgryjp/strcopy$ go test -bench .
goos: windows
goarch: amd64
pkg: github.com/sgryjp/strcopy
Benchmark_StringCopy/1MB_ScanTest-4                50000             31296 ns/op
Benchmark_StringCopy/1MB_CopyString-4           2000000000               0.34 ns/op
Benchmark_StringCopy/1MB_CopyStruct-4           2000000000               0.33 ns/op
Benchmark_StringCopy/2MB_ScanTest-4                20000             62769 ns/op
Benchmark_StringCopy/2MB_CopyString-4           2000000000               0.33 ns/op
Benchmark_StringCopy/2MB_CopyStruct-4           2000000000               0.33 ns/op
Benchmark_StringCopy/3MB_ScanTest-4                10000            117098 ns/op
Benchmark_StringCopy/3MB_CopyString-4           2000000000               0.34 ns/op
Benchmark_StringCopy/3MB_CopyStruct-4           2000000000               0.35 ns/op
Benchmark_StringCopy/4MB_ScanTest-4                10000            187655 ns/op
Benchmark_StringCopy/4MB_CopyString-4           2000000000               0.33 ns/op
Benchmark_StringCopy/4MB_CopyStruct-4           2000000000               0.33 ns/op
Benchmark_StringCopy/5MB_ScanTest-4                 5000            302590 ns/op
Benchmark_StringCopy/5MB_CopyString-4           2000000000               0.33 ns/op
Benchmark_StringCopy/5MB_CopyStruct-4           2000000000               0.33 ns/op
Benchmark_StringCopy/6MB_ScanTest-4                 3000            336664 ns/op
Benchmark_StringCopy/6MB_CopyString-4           2000000000               0.33 ns/op
Benchmark_StringCopy/6MB_CopyStruct-4           2000000000               0.32 ns/op
Benchmark_StringCopy/7MB_ScanTest-4                 3000            402793 ns/op
Benchmark_StringCopy/7MB_CopyString-4           2000000000               0.32 ns/op
Benchmark_StringCopy/7MB_CopyStruct-4           2000000000               0.34 ns/op
Benchmark_StringCopy/8MB_ScanTest-4                 3000            474773 ns/op
Benchmark_StringCopy/8MB_CopyString-4           2000000000               0.34 ns/op
Benchmark_StringCopy/8MB_CopyStruct-4           2000000000               0.33 ns/op
Benchmark_StringCopy/9MB_ScanTest-4                 2000            556626 ns/op
Benchmark_StringCopy/9MB_CopyString-4           2000000000               0.34 ns/op
Benchmark_StringCopy/9MB_CopyStruct-4           2000000000               0.34 ns/op
PASS
ok      github.com/sgryjp/strcopy       28.279s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment