-
-
Save reynir/2630866 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
$ ./command-line-arguments.test --test.bench="Bench" | |
testing: warning: no tests to run | |
PASS | |
BenchmarkConcat 200000 10877 ns/op | |
BenchmarkJoin 100000 16848 ns/op | |
BenchmarkBuffer 100000 20729 ns/op |
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" | |
import "strings" | |
import "testing" | |
func BenchmarkConcat(b *testing.B) { | |
for iter := 0; iter < b.N; iter++ { | |
var As = "AAAA" | |
var _ = As+As+As+As+As+As+As+As+As+As+As+As+As+As+As+As+As+As+As+As+As+As+As+ | |
As+As+As+As+As+As+As+As+As+As+As+As+As+As+As+As+As+As+As+As+As+As+As+ | |
As+As+As+As+As+As+As+As+As+As+As+As+As+As+As+As+As+As+As+As+As+As+As+ | |
As+As+As+As+As+As+As+As+As+As+As+As+As+As+As+As+As+As+As+As+As+As+As+ | |
As+As+As+As+As+As+As+As | |
} | |
} | |
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