Created
August 28, 2016 20:50
-
-
Save pswaminathan/a51cda43cba2f7ea2194c90e3f3e1136 to your computer and use it in GitHub Desktop.
Int Conversion Benchmarks
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 ( | |
"fmt" | |
"strconv" | |
"testing" | |
) | |
var ( | |
n = 12345 | |
s string | |
) | |
func BenchmarkFmt(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
s = fmt.Sprintf("%d", n) | |
} | |
} | |
func BenchmarkStrconv(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
s = strconv.Itoa(n) | |
} | |
} | |
/* | |
$ go test -bench=. -benchmem -v int_conv_test.go | |
testing: warning: no tests to run | |
PASS | |
BenchmarkFmt-8 5000000 251 ns/op 16 B/op 2 allocs/op | |
BenchmarkStrconv-8 20000000 67.2 ns/op 5 B/op 1 allocs/op | |
ok command-line-arguments 2.942s | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment