Skip to content

Instantly share code, notes, and snippets.

@pswaminathan
Created August 28, 2016 20:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pswaminathan/a51cda43cba2f7ea2194c90e3f3e1136 to your computer and use it in GitHub Desktop.
Save pswaminathan/a51cda43cba2f7ea2194c90e3f3e1136 to your computer and use it in GitHub Desktop.
Int Conversion Benchmarks
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