Created
December 1, 2015 09:54
-
-
Save Dieterbe/84a842fa387bd0f01658 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
~ ❯❯❯ go run test.go | |
sum 883.348µs 491499.30088724016 | |
max 683.371µs 45508.46704409849 | |
min 708.77µs -53997.430680224774 |
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" | |
"math/rand" | |
"time" | |
) | |
func main() { | |
vals := make([]float64, 1000000) | |
for i := 0; i < len(vals); i++ { | |
vals[i] = rand.NormFloat64() * 10000 | |
} | |
pre := time.Now() | |
sum := float64(0) | |
for i := 0; i < len(vals); i++ { | |
sum += vals[i] | |
} | |
fmt.Println("sum", time.Now().Sub(pre), sum) | |
pre = time.Now() | |
max := -math.MaxFloat64 | |
for i := 0; i < len(vals); i++ { | |
if vals[i] > max { | |
max = vals[i] | |
} | |
} | |
fmt.Println("max", time.Now().Sub(pre), max) | |
pre = time.Now() | |
min := math.MaxFloat64 | |
for i := 0; i < len(vals); i++ { | |
if vals[i] < min { | |
min = vals[i] | |
} | |
} | |
fmt.Println("min", time.Now().Sub(pre), min) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment