Skip to content

Instantly share code, notes, and snippets.

@evalphobia
Last active February 13, 2023 16:12
Show Gist options
  • Save evalphobia/caee1602969a640a4530 to your computer and use it in GitHub Desktop.
Save evalphobia/caee1602969a640a4530 to your computer and use it in GitHub Desktop.
golang benchmark: String and Int conversions
package bench
import (
"strconv"
"testing"
)
var smallStr = "35"
var bigStr = "999999999999999"
func BenchmarkAtoi(b *testing.B) {
for i := 0; i < b.N; i++ {
val, _ := strconv.Atoi(smallStr)
_ = val
}
}
func BenchmarkAtoiParseInt(b *testing.B) {
for i := 0; i < b.N; i++ {
val, _ := strconv.ParseInt(smallStr, 0, 64)
_ = val
}
}
func BenchmarkAtoiBig(b *testing.B) {
for i := 0; i < b.N; i++ {
val, _ := strconv.Atoi(bigStr)
_ = val
}
}
func BenchmarkAtoiParseIntBig(b *testing.B) {
for i := 0; i < b.N; i++ {
val, _ := strconv.ParseInt(bigStr, 0, 64)
_ = val
}
}
package bench
import (
"fmt"
"strconv"
"testing"
)
var smallInt = 35
var bigInt = 999999999999999
func BenchmarkItoa(b *testing.B) {
for i := 0; i < b.N; i++ {
val := strconv.Itoa(smallInt)
_ = val
}
}
func BenchmarkItoaFormatInt(b *testing.B) {
for i := 0; i < b.N; i++ {
val := strconv.FormatInt(int64(smallInt), 10)
_ = val
}
}
func BenchmarkItoaSprint(b *testing.B) {
for i := 0; i < b.N; i++ {
val := fmt.Sprint(smallInt)
_ = val
}
}
func BenchmarkItoaSprintf(b *testing.B) {
for i := 0; i < b.N; i++ {
val := fmt.Sprintf("%d", smallInt)
_ = val
}
}
func BenchmarkItoaBig(b *testing.B) {
for i := 0; i < b.N; i++ {
val := strconv.Itoa(bigInt)
_ = val
}
}
func BenchmarkItoaFormatIntBig(b *testing.B) {
for i := 0; i < b.N; i++ {
val := strconv.FormatInt(int64(bigInt), 10)
_ = val
}
}
func BenchmarkItoaSprintBig(b *testing.B) {
for i := 0; i < b.N; i++ {
val := fmt.Sprint(bigInt)
_ = val
}
}
func BenchmarkItoaSprintfBig(b *testing.B) {
for i := 0; i < b.N; i++ {
val := fmt.Sprintf("%d", bigInt)
_ = val
}
}
$ go test -bench . -benchmem

testing: warning: no tests to run
PASS

BenchmarkAtoi                           50000000           28.6 ns/op         0 B/op           0 allocs/op
BenchmarkAtoiParseInt                   50000000           27.0 ns/op         0 B/op           0 allocs/op
BenchmarkAtoiBig                        20000000           57.2 ns/op         0 B/op           0 allocs/op
BenchmarkAtoiParseIntBig                30000000           54.6 ns/op         0 B/op           0 allocs/op

BenchmarkItoa                           30000000           52.6 ns/op         2 B/op           1 allocs/op
BenchmarkItoaFormatInt                  30000000           52.5 ns/op         2 B/op           1 allocs/op
BenchmarkItoaSprint                     5000000            254 ns/op          16 B/op          2 allocs/op
BenchmarkItoaSprintf                    5000000            299 ns/op          16 B/op          2 allocs/op
BenchmarkItoaBig                        10000000           140 ns/op          16 B/op          1 allocs/op
BenchmarkItoaFormatIntBig               10000000           118 ns/op          16 B/op          1 allocs/op
BenchmarkItoaSprintBig                  5000000            326 ns/op          24 B/op          2 allocs/op
BenchmarkItoaSprintfBig                 5000000            363 ns/op          24 B/op          2 allocs/op

ok  	github.com/evalphobia/bench/	26.708s
@ernsheong
Copy link

What does _ = val do?

@ccressent
Copy link

@ernsheong: it makes it so the compiler doesn't complain about val being declared but not used.

@alpancs
Copy link

alpancs commented Sep 5, 2017

@ccressent why not just fmt.Sprintf("%d", bigInt) without assigning it to any variables? You even don't need line _ = val.

@britishben
Copy link

It would be better if you chose random integers first, too (maybe in an init() function?) - That way you're not just checking the optimisation of the two hardcoded values you chose.

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