Created
July 24, 2012 20:46
-
-
Save ncw/3172562 to your computer and use it in GitHub Desktop.
Conversion of Cumul.java to Go
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
// Code from http://lemire.me/blog/archives/2012/07/23/is-cc-worth-it/ | |
// Converted to Go by Nick Craig-Wood | |
// Runs in pretty much identical time to the "basic sum (C++-like)" code | |
package main | |
import ( | |
"fmt" | |
"time" | |
) | |
func sum(data []int) { | |
for i := range data[1:] { | |
data[i+1] += data[i] | |
} | |
} | |
func givemeanarray(N int) []int { | |
bigarray := make([]int, N) | |
for k := range bigarray { | |
bigarray[k] = k + k/3 | |
} | |
return bigarray | |
} | |
func test(N int) { | |
for t := 0; t < 10; t++ { | |
data := givemeanarray(N) | |
bef := time.Now() | |
sum(data) | |
duration := time.Since(bef).Seconds() | |
fmt.Println(float64(N) / duration / 1E6) | |
} | |
} | |
func main() { | |
test(50 * 1000 * 1000) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment