Skip to content

Instantly share code, notes, and snippets.

@dustin
Created December 6, 2012 04:11
Show Gist options
  • Select an option

  • Save dustin/4221719 to your computer and use it in GitHub Desktop.

Select an option

Save dustin/4221719 to your computer and use it in GitHub Desktop.
various "max" benchmarks in go
package main
import (
"testing"
)
type bigStruct struct {
a int
ba1, ba2, ba3, ba4 [8192]byte
}
func cmp(a, b bigStruct, bee *testing.B) {
for i := 0; i < bee.N; i++ {
x := a
if x.a > b.a {
x = b
}
_ = &x
}
}
func cmpl(a, b bigStruct, bee *testing.B) {
for i := 0; i < bee.N; i++ {
var x bigStruct
if a.a > b.a {
x = a
} else {
x = b
}
_ = &x
}
}
func cmpp(a, b bigStruct, bee *testing.B) {
for i := 0; i < bee.N; i++ {
p := &a
if a.a > b.a {
p = &b
}
x := *p
_ = &x
}
}
func BenchmarkIfGreater(bee *testing.B) {
var a, b bigStruct
a.a = 3
b.a = 7
cmp(b, a, bee)
}
func BenchmarkIfLess(bee *testing.B) {
var a, b bigStruct
a.a = 3
b.a = 7
cmp(a, b, bee)
}
func BenchmarkLazyLess(bee *testing.B) {
var a, b bigStruct
a.a = 3
b.a = 7
cmpl(a, b, bee)
}
func BenchmarkLazyGreater(bee *testing.B) {
var a, b bigStruct
a.a = 3
b.a = 7
cmpl(b, a, bee)
}
func BenchmarkPtrLess(bee *testing.B) {
var a, b bigStruct
a.a = 3
b.a = 7
cmpp(a, b, bee)
}
func BenchmarkPtrGreater(bee *testing.B) {
var a, b bigStruct
a.a = 3
b.a = 7
cmpp(b, a, bee)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment