Skip to content

Instantly share code, notes, and snippets.

@romanitalian
Created October 30, 2020 15:36
Show Gist options
  • Save romanitalian/6660ee557e243edfaf462f8926da518f to your computer and use it in GitHub Desktop.
Save romanitalian/6660ee557e243edfaf462f8926da518f to your computer and use it in GitHub Desktop.
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