Skip to content

Instantly share code, notes, and snippets.

@nuqz
Last active July 1, 2017 21:02
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 nuqz/98e035b117bc7265e8f5945c0ed366db to your computer and use it in GitHub Desktop.
Save nuqz/98e035b117bc7265e8f5945c0ed366db to your computer and use it in GitHub Desktop.
package even_or_odd
import (
"testing"
"math"
)
type Solution func (int) string
var smartMap = map[int]string{0: "Even", 1: "Odd"}
var smartSlice = [...]string{"Even","Odd"}
const even = "Even"
const odd = "Odd"
func Naive(i int) string {
if i % 2 == 0 {
return "Even"
}
return "Odd"
}
func NaiveConst(i int) string {
if i % 2 == 0 {
return even
}
return odd
}
func NaiveExtraVar(i int) string {
if j := i % 2; j == 0 {
return "Even"
}
return "Odd"
}
func SemiSmartMapConst(i int) string {
if i % 2 == 0 {
return smartMap[0]
}
return smartMap[1]
}
func SemiSmartSliceConst(i int) string {
if i % 2 == 0 {
return smartSlice[0]
}
return smartSlice[1]
}
func Math(i int) string {
if j := math.Mod(float64(i), 2.0); j == 0 {
return "Even"
}
return "Odd"
}
func MathConst(i int) string {
if j := math.Mod(float64(i), 2.0); j == 0 {
return even
}
return odd
}
func SmartMap(i int) string {
return map[int]string{0:"Even", 1:"Odd"}[i % 2]
}
func SmartSlice(i int) string {
return [...]string{"Even","Odd"}[i % 2]
}
func SmartMapConst(i int) string {
return smartMap[i % 2]
}
func SmartSliceConst(i int) string {
return smartSlice[i % 2]
}
func benchmark(b *testing.B, s Solution) {
for n := 0; n < b.N; n++ {
for i := 0; i < 1000000; i++ {
s(i);
}
}
}
func BenchmarkNaive(b *testing.B) {
benchmark(b, Naive)
}
func BenchmarkNaiveConst(b *testing.B) {
benchmark(b, NaiveConst)
}
func BenchmarkNaiveExtraVar(b *testing.B) {
benchmark(b, NaiveExtraVar)
}
func BenchmarkSemiSmartMap(b *testing.B) {
benchmark(b, SemiSmartMapConst)
}
func BenchmarkSemiSmartSlice(b *testing.B) {
benchmark(b, SemiSmartSliceConst)
}
func BenchmarkMath(b *testing.B) {
benchmark(b, Math)
}
func BenchmarkMathConst(b *testing.B) {
benchmark(b, MathConst)
}
func BenchmarkSmartMap(b *testing.B) {
benchmark(b, SmartMap)
}
func BenchmarkSmartSlice(b *testing.B) {
benchmark(b, SmartSlice)
}
func BenchmarkSmartMapConst(b *testing.B) {
benchmark(b, SmartMapConst)
}
func BenchmarkSmartSliceConst(b *testing.B) {
benchmark(b, SmartSliceConst)
}
//
// Results:
//
// BenchmarkNaive-4 500 3947839 ns/op
// BenchmarkNaiveConst-4 500 3732272 ns/op
// BenchmarkNaiveExtraVar-4 500 3894234 ns/op
// BenchmarkSemiSmartMap-4 100 11112005 ns/op
// BenchmarkSemiSmartSlice-4 500 3865519 ns/op
// BenchmarkMath-4 5 292276168 ns/op
// BenchmarkMathConst-4 5 288597383 ns/op
// BenchmarkSmartMap-4 3 382927711 ns/op
// BenchmarkSmartSlice-4 300 4245053 ns/op
// BenchmarkSmartMapConst-4 100 11165296 ns/op
// BenchmarkSmartSliceConst-4 300 4232659 ns/op
// PASS
// ok indiesoft/codewarriors/even_or_odd 22.490s
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment