Created
October 30, 2020 15:36
-
-
Save romanitalian/6660ee557e243edfaf462f8926da518f to your computer and use it in GitHub Desktop.
This file contains hidden or 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" | |
"testing" | |
"time" | |
) | |
// slice_and_heap_allocation_test.go | |
// 460 ns/op | |
func BenchmarkSliceAndHeap(b *testing.B) { | |
b.ResetTimer() | |
rand.Seed(time.Now().UnixNano()) | |
for i := 0; i < b.N; i++ { | |
sl := make([]int, rand.Intn(500 - 1) + 1) | |
_ = sl | |
} | |
} | |
// 0.300 ns/op | |
func BenchmarkSliceAndHeap2(b *testing.B) { | |
b.ResetTimer() | |
rand.Seed(time.Now().UnixNano()) | |
for i := 0; i < b.N; i++ { | |
sl := make([]int, 1000) | |
_ = sl | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment