Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- */
Add Comment
Please, Sign In to add comment