mirror of
https://github.com/teivah/100-go-mistakes.git
synced 2026-06-21 00:47:11 +08:00
Merge pull request #28 from nikzayn/master
benchmark test added for predictability
This commit is contained in:
commit
d65c1c05dd
1 changed files with 32 additions and 0 deletions
32
12-optimizations/91-cpu-caches/predictability/main_test.go
Normal file
32
12-optimizations/91-cpu-caches/predictability/main_test.go
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
package main
|
||||
|
||||
import "testing"
|
||||
|
||||
var global int64
|
||||
|
||||
const n = 1_000_000
|
||||
|
||||
func BenchmarkLinkedList(b *testing.B) {
|
||||
var local int64
|
||||
for i := 0; i < b.N; i++ {
|
||||
b.StopTimer()
|
||||
nodes := make([]node, n)
|
||||
for i := 0; i < n-1; i++ {
|
||||
nodes[i].next = &nodes[i+1]
|
||||
}
|
||||
b.StartTimer()
|
||||
local = linkedList(&nodes[0])
|
||||
}
|
||||
global = local
|
||||
}
|
||||
|
||||
func BenchmarkSum2(b *testing.B) {
|
||||
var local int64
|
||||
for i := 0; i < b.N; i++ {
|
||||
b.StopTimer()
|
||||
s := make([]int64, n)
|
||||
b.StartTimer()
|
||||
local = sum2(s)
|
||||
}
|
||||
global = local
|
||||
}
|
||||
Loading…
Reference in a new issue