-
-
Save PyYoshi/d5bcd496ade6ec3b4b6c to your computer and use it in GitHub Desktop.
Golang string join benchmark
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
go version | |
go test -benchmem -bench . |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
func main() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"strings" | |
"testing" | |
) | |
var m = [...]string{ | |
"AAAAAAAAA", | |
"AAAAAAAAA", | |
"AAAAAAAAA", | |
"AAAAAAAAA", | |
"AAAAAAAAA", | |
"AAAAAAAAA", | |
"AAAAAAAAA", | |
} | |
func BenchmarkStringsJoin____(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
_ = strings.Join(m[:], ",") + "," | |
} | |
} | |
func BenchmarkAppendOperator_(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
var m2 string | |
for _, v := range m { | |
m2 += m2 + "," + v | |
} | |
m2 += "," | |
} | |
} | |
func BenchmarkAppendOperatorF(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
var m2 string | |
for _, v := range m { | |
m2 += v + "," | |
} | |
m2 += "," | |
} | |
} | |
func BenchmarkHardCoding_____(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
_ = m[0] + "," + m[1] + "," + m[2] + "," + m[3] + "," + m[4] + "," + m[5] + "," + m[6] | |
} | |
} | |
func BenchmarkByteArray______(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
var m2 []byte | |
for _, v := range m { | |
m2 = append(m2, v...) | |
m2 = append(m2, ',') | |
} | |
_ = string(m2) | |
} | |
} | |
func BenchmarkCapByteArray___(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
var m2 = make([]byte, 0, 100) | |
for _, v := range m { | |
m2 = append(m2, v...) | |
m2 = append(m2, ',') | |
} | |
_ = string(m2) | |
} | |
} | |
func BenchmarkBytesBuffer____(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
var m2 bytes.Buffer | |
for _, v := range m { | |
m2.Write([]byte(v)) | |
m2.Write([]byte{','}) | |
} | |
_ = m2.String() | |
} | |
} | |
func BenchmarkCapBytesBuffer_(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
var m2 = bytes.NewBuffer(make([]byte, 0, 100)) | |
for _, v := range m { | |
m2.Write([]byte(v)) | |
m2.Write([]byte{','}) | |
} | |
_ = m2.String() | |
} | |
} | |
func BenchmarkCapBytesBuffer2(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
var m2 = bytes.NewBuffer(make([]byte, 0, 100)) | |
for _, v := range m { | |
m2.WriteString(v) | |
m2.WriteString(",") | |
} | |
_ = m2.String() | |
} | |
} |
Author
PyYoshi
commented
Jul 30, 2015
go1.5beta3
./run.sh
go version devel +d3ffc97 Wed Jul 29 23:50:20 2015 +0000 linux/amd64
testing: warning: no tests to run
PASS
BenchmarkStringsJoin____-4 5000000 357 ns/op 240 B/op 3 allocs/op
BenchmarkAppendOperator_-4 1000000 1985 ns/op 3808 B/op 8 allocs/op
BenchmarkAppendOperatorF-4 2000000 837 ns/op 416 B/op 8 allocs/op
BenchmarkHardCoding_____-4 5000000 276 ns/op 80 B/op 1 allocs/op
BenchmarkByteArray______-4 3000000 518 ns/op 320 B/op 5 allocs/op
BenchmarkCapByteArray___-4 10000000 148 ns/op 80 B/op 1 allocs/op
BenchmarkBytesBuffer____-4 2000000 716 ns/op 336 B/op 3 allocs/op
BenchmarkCapBytesBuffer_-4 3000000 563 ns/op 304 B/op 3 allocs/op
BenchmarkCapBytesBuffer2-4 3000000 486 ns/op 304 B/op 3 allocs/op
./run.sh
go version go1.6.1 darwin/amd64
testing: warning: no tests to run
PASS
BenchmarkStringsJoin____-4 5000000 347 ns/op 240 B/op 3 allocs/op
BenchmarkAppendOperator_-4 1000000 1805 ns/op 3808 B/op 8 allocs/op
BenchmarkAppendOperatorF-4 2000000 760 ns/op 416 B/op 8 allocs/op
BenchmarkHardCoding_____-4 5000000 265 ns/op 80 B/op 1 allocs/op
BenchmarkByteArray______-4 3000000 455 ns/op 320 B/op 5 allocs/op
BenchmarkCapByteArray___-4 10000000 134 ns/op 80 B/op 1 allocs/op
BenchmarkBytesBuffer____-4 2000000 666 ns/op 336 B/op 3 allocs/op
BenchmarkCapBytesBuffer_-4 3000000 519 ns/op 304 B/op 3 allocs/op
BenchmarkCapBytesBuffer2-4 3000000 441 ns/op 304 B/op 3 allocs/op
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment