Created
June 26, 2015 17:56
-
-
Save CodeMonk/8ea4313ed4e0da11895f to your computer and use it in GitHub Desktop.
This file contains 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" | |
) | |
func BenchmarkConcat(b *testing.B) { | |
var str string | |
for n := 0; n < b.N; n++ { | |
str += "x" | |
} | |
b.StopTimer() | |
if s := strings.Repeat("x", b.N); str != s { | |
b.Errorf("unexpected result; got=%s, want=%s", str, s) | |
} | |
} | |
func BenchmarkBuffer(b *testing.B) { | |
var buffer bytes.Buffer | |
for n := 0; n < b.N; n++ { | |
buffer.WriteString("x") | |
} | |
out := buffer.String() | |
b.StopTimer() | |
if s := strings.Repeat("x", b.N); out != s { | |
b.Errorf("unexpected result; got=%s, want=%s", out, s) | |
} | |
} | |
func BenchmarkCopy(b *testing.B) { | |
bs := make([]byte, b.N) | |
bl := 0 | |
b.ResetTimer() | |
for n := 0; n < b.N; n++ { | |
bl += copy(bs[bl:], "x") | |
} | |
out := string(bs) | |
b.StopTimer() | |
if s := strings.Repeat("x", b.N); out != s { | |
b.Errorf("unexpected result; got=%s, want=%s", out, s) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment