Created
August 27, 2019 18:07
-
-
Save lorinbeer/3f36fd80af71c61298ef60ba8b7136db to your computer and use it in GitHub Desktop.
preconvert vs in place conversion benchmark
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 ( | |
"testing" | |
) | |
type myKindaString string | |
// do nothing ops, no we don't need three | |
func doSumptin(a string) {} | |
func BenchmarkConversion(b *testing.B) { | |
var mine myKindaString | |
mine = "crapola" | |
for n := 0; n < b.N; n++ { | |
doSumptin(string(mine)) | |
} | |
} | |
func BenchmarkPreConvert(b *testing.B) { | |
var mine myKindaString | |
mine = "sumtin" | |
sMine := string(mine) | |
for n := 0; n < b.N; n++ { | |
doSumptin(sMine) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$ go test -bench=.
goos: darwin
goarch: amd64
BenchmarkConversion-8 2000000000 0.30 ns/op
BenchmarkPreConvert-8 2000000000 0.60 ns/op