Skip to content

Instantly share code, notes, and snippets.

@ncw
Created July 24, 2012 20:46
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 ncw/3172562 to your computer and use it in GitHub Desktop.
Save ncw/3172562 to your computer and use it in GitHub Desktop.
Conversion of Cumul.java to Go
// 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