Skip to content

Instantly share code, notes, and snippets.

@richardartoul
Last active October 23, 2017 17:35
Show Gist options
  • Save richardartoul/2ce3c5258df0fb6f47a030950eafe93e to your computer and use it in GitHub Desktop.
Save richardartoul/2ce3c5258df0fb6f47a030950eafe93e to your computer and use it in GitHub Desktop.
Benchmark map[time.Time] vs map[int64]
package main
import (
"testing"
"time"
)
// Package-level variables to prevent compiler optimizations
var timeMapSum int
var int64MapSum int
var lastSeenTime time.Time
func BenchmarkMapAccessTime(b *testing.B) {
// Prepare datasets outside of the benchmark
timeSlice := []time.Time{}
for n := 0; n < 100000000; n++ {
timeSlice = append(timeSlice, time.Unix(int64(n), 0))
}
timeMap := map[time.Time]int{}
b.Run("time.Time", func(b *testing.B) {
// Writes
for n := 0; n < b.N; n++ {
timeStruct := timeSlice[n]
timeMap[timeStruct] = n
}
// Reads
for n := 0; n < b.N; n++ {
timeStruct := timeSlice[n]
lastSeenTime = timeStruct
timeMapSum += timeMap[timeStruct]
}
})
// To account for the fact that maintaining the existing interfaces may involve
// additional calls to .UnixNano() and .Unix() we include a call to each on
// both the write and read path to benchmark the worst case
int64Map := map[int64]int{}
b.Run("int64", func(b *testing.B) {
// Writes
for n := 0; n < b.N; n++ {
n64 := int64(timeSlice[n].UnixNano())
lastSeenTime = time.Unix(0, n64)
int64Map[n64] = n
int64MapSum += int64Map[n64]
}
// Reads
for n := 0; n < b.N; n++ {
n64 := int64(timeSlice[n].UnixNano())
lastSeenTime = time.Unix(0, n64)
int64MapSum += int64Map[n64]
}
})
}
/*
go test --bench=. --benchtime=10s --count=10 --parallel=1 --cpu=1 -benchmem benchmark_time.go
BenchmarkMapAccessTime/time.Time 30000000 1095 ns/op 164 B/op 0 allocs/op
BenchmarkMapAccessTime/time.Time 30000000 482 ns/op 0 B/op 0 allocs/op
BenchmarkMapAccessTime/time.Time 30000000 477 ns/op 0 B/op 0 allocs/op
BenchmarkMapAccessTime/time.Time 30000000 476 ns/op 0 B/op 0 allocs/op
BenchmarkMapAccessTime/time.Time 30000000 480 ns/op 0 B/op 0 allocs/op
BenchmarkMapAccessTime/time.Time 30000000 480 ns/op 0 B/op 0 allocs/op
BenchmarkMapAccessTime/time.Time 30000000 476 ns/op 0 B/op 0 allocs/op
BenchmarkMapAccessTime/time.Time 30000000 476 ns/op 0 B/op 0 allocs/op
BenchmarkMapAccessTime/time.Time 30000000 473 ns/op 0 B/op 0 allocs/op
BenchmarkMapAccessTime/time.Time 30000000 472 ns/op 0 B/op 0 allocs/op
BenchmarkMapAccessTime/int64 50000000 505 ns/op 57 B/op 0 allocs/op
BenchmarkMapAccessTime/int64 50000000 361 ns/op 0 B/op 0 allocs/op
BenchmarkMapAccessTime/int64 50000000 357 ns/op 0 B/op 0 allocs/op
BenchmarkMapAccessTime/int64 50000000 357 ns/op 0 B/op 0 allocs/op
BenchmarkMapAccessTime/int64 50000000 356 ns/op 0 B/op 0 allocs/op
BenchmarkMapAccessTime/int64 50000000 352 ns/op 0 B/op 0 allocs/op
BenchmarkMapAccessTime/int64 50000000 355 ns/op 0 B/op 0 allocs/op
BenchmarkMapAccessTime/int64 50000000 355 ns/op 0 B/op 0 allocs/op
BenchmarkMapAccessTime/int64 50000000 353 ns/op 0 B/op 0 allocs/op
BenchmarkMapAccessTime/int64 50000000 352 ns/op 0 B/op 0 allocs/op
PASS
ok command-line-arguments 494.040s
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment