Skip to content

Instantly share code, notes, and snippets.

@rogpeppe
Created February 21, 2011 17:07
Show Gist options
  • Save rogpeppe/837349 to your computer and use it in GitHub Desktop.
Save rogpeppe/837349 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"runtime"
"testing"
)
func main() {
fmt.Printf("add %v\n", testing.Benchmark(benchAdd))
fmt.Printf("del %v\n", testing.Benchmark(benchDel))
}
const N = 1e6
func benchAdd(b *testing.B) {
for i := b.N-1; i >= 0; i-- {
b.StopTimer()
runtime.GC()
m := make(map[int]int)
b.StartTimer()
for j := 0; j < N; j++ {
m[j] = 0
}
}
}
func benchDel(b *testing.B) {
for i := b.N-1; i >= 0; i-- {
b.StopTimer()
m := make(map[int]int)
for j := 0; j < N; j++ {
m[j] = 0
}
runtime.GC()
b.StartTimer()
for j := 0; j < N; j++ {
m[j] = 0, false
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment