Last active
June 30, 2022 17:19
-
-
Save teivah/36e2ff27b48b9e7526e0894fe91ef0b0 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
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 | |
} |
Author
teivah
commented
Oct 24, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment