Skip to content

Instantly share code, notes, and snippets.

View percybolmer's full-sized avatar

ProgrammingPercy percybolmer

View GitHub Profile
package benching
import (
"testing"
)
func BenchmarkSimplest(b *testing.B) {
}
// B is a type passed to Benchmark functions to manage benchmark
// timing and to specify the number of iterations to run.
//
// A benchmark ends when its Benchmark function returns or calls any of the methods
// FailNow, Fatal, Fatalf, SkipNow, Skip, or Skipf. Those methods must be called
// only from the goroutine running the Benchmark function.
// The other reporting methods, such as the variations of Log and Error,
// may be called simultaneously from multiple goroutines.
//
// Like in tests, benchmark logs are accumulated during execution
// BenchmarkSimplestNTimes runs a benchmark N amount of times.
func BenchmarkSimplestNTimes(b *testing.B) {
for i := 0; i < b.N; i++ {
// Run function to benchmark here
}
}
// insertXIntMap is used to add X amount of items into a Map[int]int
func insertXIntMap(x int, b *testing.B) {
// Initialize Map and Insert X amount of items
testmap := make(map[int]int, 0)
// Reset timer after Initalizing map, that's not what we want to test
b.ResetTimer()
for i := 0; i < x; i++ {
// Insert value of I into I key.
testmap[i] = i
}
// BenchmarkInsertIntMap100000 benchmarks the speed of inserting 100000 integers into the map.
func BenchmarkInsertIntMap100000(b *testing.B) {
for i := 0; i < b.N; i++ {
insertXIntMap(100000, b)
}
}
// BenchmarkInsertIntMap10000 benchmarks the speed of inserting 10000 integers into the map.
func BenchmarkInsertIntMap10000(b *testing.B) {
package benching
import (
"testing"
)
// insertXInterfaceMap is used to add X amount of items into a Map[interface]int
func insertXInterfaceMap(x int, b *testing.B) {
// Initialize Map and Insert X amount of items
testmap := make(map[interface{}]int, 0)
// insertXPreallocIntMap is used to add X amount of items into a Map[int]int
func insertXPreallocIntMap(x int, b *testing.B) {
// Initialize Map and Insert X amount of items and Prealloc the size to X
testmap := make(map[int]int, x)
// Reset timer after Initalizing map, that's not what we want to test
b.ResetTimer()
for i := 0; i < x; i++ {
// Insert value of I into I key.
testmap[i] = i
}
package benching
import (
"testing"
)
// insertXPreallocIntMap is used to add X amount of items into a Map[int]int
func insertXPreallocIntMap(x int, b *testing.B) {
// Initialize Map and Insert X amount of items and Prealloc the size to X
testmap := make(map[int]int, x)
// insertXIntSlice is used to add X amount of items into a []int
func insertXIntSlice(x int, b *testing.B) {
// Initalize a Slice and insert X amount of Items
testSlice := make([]int, 0)
// reset timer
b.ResetTimer()
for i := 0; i < x; i++ {
testSlice = append(testSlice, i)
}
}
// insertXPreallocIntSlice is used to add X amount of items into a []int
func insertXPreallocIntSlice(x int, b *testing.B) {
// Initalize a Slice and insert X amount of Items
testSlice := make([]int, x)
// reset timer
b.ResetTimer()
for i := 0; i < x; i++ {
testSlice[i] = i
}
}