Skip to content

Instantly share code, notes, and snippets.

@ncw
Created Jul 24, 2012
Embed
What would you like to do?
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