github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/bench/sortedset/sortedset_test.go (about) 1 package sortedset 2 3 import ( 4 "math/rand" 5 "testing" 6 ) 7 8 func newBenchSet() *Set { 9 set := New() 10 base := 0 11 rand.Seed(0) 12 for i := 0; i < 1e4; i += 1 { 13 base += rand.Intn(15) + 1 14 set.Add(base) 15 } 16 return set 17 } 18 19 func BenchmarkChanUnbuffered(b *testing.B) { 20 set := newBenchSet() 21 b.ResetTimer() 22 total := 0 23 for i := 0; i < b.N; i++ { 24 for v := range set.IterChan(0) { 25 total += v 26 } 27 } 28 _ = total 29 } 30 31 func BenchmarkBuffered8(b *testing.B) { 32 set := newBenchSet() 33 b.ResetTimer() 34 total := 0 35 for i := 0; i < b.N; i++ { 36 for v := range set.IterChan(8) { 37 total += v 38 } 39 } 40 _ = total 41 } 42 43 func BenchmarkBuffered16(b *testing.B) { 44 set := newBenchSet() 45 b.ResetTimer() 46 total := 0 47 for i := 0; i < b.N; i++ { 48 for v := range set.IterChan(16) { 49 total += v 50 } 51 } 52 _ = total 53 } 54 55 func BenchmarkBuffered32(b *testing.B) { 56 set := newBenchSet() 57 b.ResetTimer() 58 total := 0 59 for i := 0; i < b.N; i++ { 60 for v := range set.IterChan(32) { 61 total += v 62 } 63 } 64 _ = total 65 } 66 67 func BenchmarkBuffered64(b *testing.B) { 68 set := newBenchSet() 69 b.ResetTimer() 70 total := 0 71 for i := 0; i < b.N; i++ { 72 for v := range set.IterChan(64) { 73 total += v 74 } 75 } 76 _ = total 77 } 78 79 func BenchmarkSlice(b *testing.B) { 80 set := newBenchSet() 81 b.ResetTimer() 82 total := 0 83 for i := 0; i < b.N; i++ { 84 for _, v := range set.IterSlice() { 85 total += v 86 } 87 } 88 _ = total 89 } 90 91 func BenchmarkCallback(b *testing.B) { 92 set := newBenchSet() 93 b.ResetTimer() 94 total := 0 95 for i := 0; i < b.N; i++ { 96 set.IterCallback(func(v int) { 97 total += v 98 }) 99 } 100 _ = total 101 } 102 103 func BenchmarkBlockCallback8(b *testing.B) { 104 set := newBenchSet() 105 b.ResetTimer() 106 total := 0 107 for i := 0; i < b.N; i++ { 108 set.IterBlockCallback(8, func(vs []int) { 109 for v := range vs { 110 total += v 111 } 112 }) 113 } 114 _ = total 115 } 116 117 func BenchmarkBlockCallback16(b *testing.B) { 118 set := newBenchSet() 119 b.ResetTimer() 120 total := 0 121 for i := 0; i < b.N; i++ { 122 set.IterBlockCallback(16, func(vs []int) { 123 for v := range vs { 124 total += v 125 } 126 }) 127 } 128 _ = total 129 } 130 131 func BenchmarkBlockCallback32(b *testing.B) { 132 set := newBenchSet() 133 b.ResetTimer() 134 total := 0 135 for i := 0; i < b.N; i++ { 136 set.IterBlockCallback(32, func(vs []int) { 137 for v := range vs { 138 total += v 139 } 140 }) 141 } 142 _ = total 143 } 144 145 func BenchmarkBlockCallback64(b *testing.B) { 146 set := newBenchSet() 147 b.ResetTimer() 148 total := 0 149 for i := 0; i < b.N; i++ { 150 set.IterBlockCallback(64, func(vs []int) { 151 for v := range vs { 152 total += v 153 } 154 }) 155 } 156 _ = total 157 }