Created
February 10, 2014 22:23
-
-
Save akira093/8925475 to your computer and use it in GitHub Desktop.
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 ( | |
"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