Skip to content

Instantly share code, notes, and snippets.

@fohte
Created June 28, 2017 06:41
Show Gist options
  • Save fohte/52ff2d207bd0d81fd6e0557c2108124d to your computer and use it in GitHub Desktop.
Save fohte/52ff2d207bd0d81fd6e0557c2108124d to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"os"
"strconv"
"strings"
"testing"
)
var loopnum int = 1000000
func BenchmarkPrint_Join(b *testing.B) {
strs := makeDummyStrings(loopnum)
b.ResetTimer()
fmt.Fprintln(os.Stderr, strings.Join(strs, "\n"))
}
func BenchmarkPrint_Foreach(b *testing.B) {
strs := makeDummyStrings(loopnum)
b.ResetTimer()
for _, s := range strs {
fmt.Fprintln(os.Stderr, s)
}
}
func makeDummyStrings(n int) []string {
strs := make([]string, n)
for i := 0; i < n; i++ {
strs[i] = strconv.Itoa(i)
}
return strs
}
@fohte
Copy link
Author

fohte commented Jun 28, 2017

$ go test -bench . -benchmem 2> /dev/null
BenchmarkPrint_Join-4           2000000000               0.01 ns/op            0 B/op          0 allocs/op
BenchmarkPrint_Foreach-4        1000000000               0.55 ns/op            0 B/op          0 allocs/op
PASS
ok      ...  12.929s

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