Guest User

Untitled

a guest
Oct 24th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "testing"
  5. "time"
  6. )
  7.  
  8. // Package-level variables to prevent compiler optimizations
  9. var timeMapSum int
  10. var int64MapSum int
  11. var lastSeenTime time.Time
  12.  
  13. func BenchmarkMapAccessTime(b *testing.B) {
  14. // Prepare datasets outside of the benchmark
  15. timeSlice := []time.Time{}
  16. for n := 0; n < 100000000; n++ {
  17. timeSlice = append(timeSlice, time.Unix(int64(n), 0))
  18. }
  19.  
  20. timeMap := map[time.Time]int{}
  21. b.Run("time.Time", func(b *testing.B) {
  22. // Writes
  23. for n := 0; n < b.N; n++ {
  24. timeStruct := timeSlice[n]
  25. timeMap[timeStruct] = n
  26. }
  27.  
  28. // Reads
  29. for n := 0; n < b.N; n++ {
  30. timeStruct := timeSlice[n]
  31. lastSeenTime = timeStruct
  32. timeMapSum += timeMap[timeStruct]
  33. }
  34. })
  35.  
  36. // To account for the fact that maintaining the existing interfaces may involve
  37. // additional calls to .UnixNano() and .Unix() we include a call to each on
  38. // both the write and read path to benchmark the worst case
  39. int64Map := map[int64]int{}
  40. b.Run("int64", func(b *testing.B) {
  41. // Writes
  42. for n := 0; n < b.N; n++ {
  43. n64 := int64(timeSlice[n].UnixNano())
  44. lastSeenTime = time.Unix(0, n64)
  45. int64Map[n64] = n
  46. int64MapSum += int64Map[n64]
  47. }
  48.  
  49. // Reads
  50. for n := 0; n < b.N; n++ {
  51. n64 := int64(timeSlice[n].UnixNano())
  52. lastSeenTime = time.Unix(0, n64)
  53. int64MapSum += int64Map[n64]
  54. }
  55. })
  56. }
  57.  
  58. /*
  59. go test --bench=. --benchtime=10s --count=10 --parallel=1 --cpu=1 -benchmem benchmark_time.go
  60. BenchmarkMapAccessTime/time.Time 30000000 1095 ns/op 164 B/op 0 allocs/op
  61. BenchmarkMapAccessTime/time.Time 30000000 482 ns/op 0 B/op 0 allocs/op
  62. BenchmarkMapAccessTime/time.Time 30000000 477 ns/op 0 B/op 0 allocs/op
  63. BenchmarkMapAccessTime/time.Time 30000000 476 ns/op 0 B/op 0 allocs/op
  64. BenchmarkMapAccessTime/time.Time 30000000 480 ns/op 0 B/op 0 allocs/op
  65. BenchmarkMapAccessTime/time.Time 30000000 480 ns/op 0 B/op 0 allocs/op
  66. BenchmarkMapAccessTime/time.Time 30000000 476 ns/op 0 B/op 0 allocs/op
  67. BenchmarkMapAccessTime/time.Time 30000000 476 ns/op 0 B/op 0 allocs/op
  68. BenchmarkMapAccessTime/time.Time 30000000 473 ns/op 0 B/op 0 allocs/op
  69. BenchmarkMapAccessTime/time.Time 30000000 472 ns/op 0 B/op 0 allocs/op
  70. BenchmarkMapAccessTime/int64 50000000 505 ns/op 57 B/op 0 allocs/op
  71. BenchmarkMapAccessTime/int64 50000000 361 ns/op 0 B/op 0 allocs/op
  72. BenchmarkMapAccessTime/int64 50000000 357 ns/op 0 B/op 0 allocs/op
  73. BenchmarkMapAccessTime/int64 50000000 357 ns/op 0 B/op 0 allocs/op
  74. BenchmarkMapAccessTime/int64 50000000 356 ns/op 0 B/op 0 allocs/op
  75. BenchmarkMapAccessTime/int64 50000000 352 ns/op 0 B/op 0 allocs/op
  76. BenchmarkMapAccessTime/int64 50000000 355 ns/op 0 B/op 0 allocs/op
  77. BenchmarkMapAccessTime/int64 50000000 355 ns/op 0 B/op 0 allocs/op
  78. BenchmarkMapAccessTime/int64 50000000 353 ns/op 0 B/op 0 allocs/op
  79. BenchmarkMapAccessTime/int64 50000000 352 ns/op 0 B/op 0 allocs/op
  80. PASS
  81. ok command-line-arguments 494.040s
  82. */
Add Comment
Please, Sign In to add comment