Skip to content

Instantly share code, notes, and snippets.

@akira093
Created February 10, 2014 22:23
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 akira093/8925475 to your computer and use it in GitHub Desktop.
Save akira093/8925475 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"math"
"time"
)
func sum(a []int) int {
s := 0
for i := 0; i < len(a); i++ {
s += a[i]
}
return s
}
func sum2(a []int) int {
s := 0
for _, x := range a {
s += x
}
return s
}
func timeit(f func([]int) int) {
num := uint64(math.Pow(2, 16))
xs := make([]int, num)
now := time.Now()
for i := 0; i < 10000; i++ {
f(xs)
}
fmt.Println(time.Now().Sub(now))
}
func main() {
timeit(sum) // 528ms
timeit(sum2) // 385m
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment