Created
May 7, 2012 21:54
-
-
Save awreece/2630722 to your computer and use it in GitHub Desktop.
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
$ ./command-line-arguments.test --test.bench="Bench" | |
testing: warning: no tests to run | |
PASS | |
BenchmarkConcat 50000 46822 ns/op | |
BenchmarkJoin 200000 9121 ns/op | |
BenchmarkBuffer 200000 10764 ns/op |
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" | |
import "strings" | |
import "testing" | |
func BenchmarkConcat(b *testing.B) { | |
for iter := 0; iter < b.N; iter++ { | |
s := "" | |
for i := 0; i < 100; i++ { | |
s += "AAAA" | |
} | |
} | |
} | |
func BenchmarkJoin(b *testing.B) { | |
for iter := 0; iter < b.N; iter++ { | |
sa := make([]string, 100) | |
for i := 0; i < 100; i++ { | |
sa[i] = "AAAA" | |
} | |
strings.Join(sa, "") | |
} | |
} | |
func BenchmarkBuffer(b *testing.B) { | |
for iter := 0; iter < b.N; iter++ { | |
b := new(bytes.Buffer) | |
for i := 0; i < 100; i++ { | |
b.WriteString("AAAA") | |
} | |
b.String() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment