Skip to content

Instantly share code, notes, and snippets.

@C-Pro
Created March 1, 2021 07:36
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 C-Pro/065adec5a3211f2ed5a08651ac3345e4 to your computer and use it in GitHub Desktop.
Save C-Pro/065adec5a3211f2ed5a08651ac3345e4 to your computer and use it in GitHub Desktop.
package main_test
import (
"testing"
)
func cmp(a, b string) bool {
if len(a) != len(b) {
return false
}
for i := 0; i < len(a); i++ {
if a[i] != b[i] {
return false
}
}
return true
}
func eq(a, b string) bool { return a == b }
func BenchmarkCmp0(b *testing.B) {
for i := 0; i < b.N; i++ {
cmp("012345", "543210")
}
}
func BenchmarkCmp1(b *testing.B) {
for i := 0; i < b.N; i++ {
cmp("012345", "054321")
}
}
func BenchmarkEQ0(b *testing.B) {
for i := 0; i < b.N; i++ {
eq("012345", "543210")
}
}
func BenchmarkEQ1(b *testing.B) {
for i := 0; i < b.N; i++ {
eq("012345", "054321")
}
}
@C-Pro
Copy link
Author

C-Pro commented Mar 1, 2021

goos: linux
goarch: amd64
cpu: Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz
BenchmarkCmp0
BenchmarkCmp0-8   	1000000000	         0.7443 ns/op
BenchmarkCmp1
BenchmarkCmp1-8   	926680519	         1.310 ns/op
BenchmarkEQ0
BenchmarkEQ0-8    	320995281	         3.656 ns/op
BenchmarkEQ1
BenchmarkEQ1-8    	333738698	         3.505 ns/op
PASS

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