Skip to content

Instantly share code, notes, and snippets.

@teivah
Last active June 30, 2022 17:19
Show Gist options
  • Save teivah/36e2ff27b48b9e7526e0894fe91ef0b0 to your computer and use it in GitHub Desktop.
Save teivah/36e2ff27b48b9e7526e0894fe91ef0b0 to your computer and use it in GitHub Desktop.
package main
import (
"math/rand"
"strconv"
"testing"
)
var res int64
func BenchmarkSum(b *testing.B) {
var sum int64
benchmarks := []int{1024, 1025}
for _, bm := range benchmarks {
s := createMatrix(1000, bm)
b.Run(strconv.Itoa(bm), func(b *testing.B) {
for i := 0; i < b.N; i++ {
sum = calculateSum(s)
}
res = sum
})
}
}
func calculateSum(s [][]int64) int64 {
var sum int64
for i := 0; i < len(s); i++ {
for j := 0; j < 8; j++ {
sum += s[i][j]
}
}
return sum
}
func createMatrix(rows, columns int) [][]int64 {
s := make([][]int64, rows)
for i := 0; i < rows; i++ {
s[i] = make([]int64, columns)
for j := 0; j < columns; j++ {
s[i][j] = rand.Int63()
}
}
return s
}
@teivah
Copy link
Author

teivah commented Oct 24, 2021

goos: darwin
goarch: amd64
cpu: Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
BenchmarkSum
BenchmarkSum/1024
BenchmarkSum/1024-4         	   62149	     19349 ns/op
BenchmarkSum/1025
BenchmarkSum/1025-4         	  114414	     10093 ns/op

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment