-
Notifications
You must be signed in to change notification settings - Fork 275
/
bench_test.go
787 lines (624 loc) · 17.5 KB
/
bench_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
/*
Open Source Initiative OSI - The MIT License (MIT):Licensing
The MIT License (MIT)
Copyright (c) 2013 - 2022 Ralph Caraveo ([email protected])
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package mapset
import (
"math/rand"
"testing"
)
func nrand(n int) []int {
i := make([]int, n)
for ind := range i {
i[ind] = rand.Int()
}
return i
}
func benchAdd(b *testing.B, n int, newSet func(...int) Set[int]) {
nums := nrand(n)
b.ResetTimer()
for i := 0; i < b.N; i++ {
s := newSet()
for _, v := range nums {
s.Add(v)
}
}
}
func BenchmarkAddSafe(b *testing.B) {
benchAdd(b, 1000, NewSet[int])
}
func BenchmarkAddUnsafe(b *testing.B) {
benchAdd(b, 1000, NewThreadUnsafeSet[int])
}
func benchRemove(b *testing.B, s Set[int]) {
nums := nrand(b.N)
for _, v := range nums {
s.Add(v)
}
b.ResetTimer()
for _, v := range nums {
s.Remove(v)
}
}
func BenchmarkRemoveSafe(b *testing.B) {
benchRemove(b, NewSet[int]())
}
func BenchmarkRemoveUnsafe(b *testing.B) {
benchRemove(b, NewThreadUnsafeSet[int]())
}
func benchCardinality(b *testing.B, s Set[int]) {
for i := 0; i < b.N; i++ {
s.Cardinality()
}
}
func BenchmarkCardinalitySafe(b *testing.B) {
benchCardinality(b, NewSet[int]())
}
func BenchmarkCardinalityUnsafe(b *testing.B) {
benchCardinality(b, NewThreadUnsafeSet[int]())
}
func benchClear(b *testing.B, s Set[int]) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
s.Clear()
}
}
func BenchmarkClearSafe(b *testing.B) {
benchClear(b, NewSet[int]())
}
func BenchmarkClearUnsafe(b *testing.B) {
benchClear(b, NewThreadUnsafeSet[int]())
}
func benchClone(b *testing.B, n int, s Set[int]) {
nums := nrand(n)
for _, v := range nums {
s.Add(v)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
s.Clone()
}
}
func BenchmarkClone1Safe(b *testing.B) {
benchClone(b, 1, NewSet[int]())
}
func BenchmarkClone1Unsafe(b *testing.B) {
benchClone(b, 1, NewThreadUnsafeSet[int]())
}
func BenchmarkClone10Safe(b *testing.B) {
benchClone(b, 10, NewSet[int]())
}
func BenchmarkClone10Unsafe(b *testing.B) {
benchClone(b, 10, NewThreadUnsafeSet[int]())
}
func BenchmarkClone100Safe(b *testing.B) {
benchClone(b, 100, NewSet[int]())
}
func BenchmarkClone100Unsafe(b *testing.B) {
benchClone(b, 100, NewThreadUnsafeSet[int]())
}
func benchContains(b *testing.B, n int, s Set[int]) {
nums := nrand(n)
for _, v := range nums {
s.Add(v)
}
nums[n-1] = -1 // Definitely not in s
b.ResetTimer()
for i := 0; i < b.N; i++ {
s.Contains(nums...)
}
}
func BenchmarkContains1Safe(b *testing.B) {
benchContains(b, 1, NewSet[int]())
}
func BenchmarkContains1Unsafe(b *testing.B) {
benchContains(b, 1, NewThreadUnsafeSet[int]())
}
func BenchmarkContains10Safe(b *testing.B) {
benchContains(b, 10, NewSet[int]())
}
func BenchmarkContains10Unsafe(b *testing.B) {
benchContains(b, 10, NewThreadUnsafeSet[int]())
}
func BenchmarkContains100Safe(b *testing.B) {
benchContains(b, 100, NewSet[int]())
}
func BenchmarkContains100Unsafe(b *testing.B) {
benchContains(b, 100, NewThreadUnsafeSet[int]())
}
func benchContainsOne(b *testing.B, n int, s Set[int]) {
nums := nrand(n)
for _, v := range nums {
s.Add(v)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
s.ContainsOne(-1)
}
}
func BenchmarkContainsOne1Safe(b *testing.B) {
benchContainsOne(b, 1, NewSet[int]())
}
func BenchmarkContainsOne1Unsafe(b *testing.B) {
benchContainsOne(b, 1, NewThreadUnsafeSet[int]())
}
func BenchmarkContainsOne10Safe(b *testing.B) {
benchContainsOne(b, 10, NewSet[int]())
}
func BenchmarkContainsOne10Unsafe(b *testing.B) {
benchContainsOne(b, 10, NewThreadUnsafeSet[int]())
}
func BenchmarkContainsOne100Safe(b *testing.B) {
benchContainsOne(b, 100, NewSet[int]())
}
func BenchmarkContainsOne100Unsafe(b *testing.B) {
benchContainsOne(b, 100, NewThreadUnsafeSet[int]())
}
// In this scenario, Contains argument escapes to the heap, while ContainsOne does not.
func benchContainsComparison(b *testing.B, n int, s Set[int]) {
nums := nrand(n)
for _, v := range nums {
s.Add(v)
}
b.Run("Contains", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for _, v := range nums {
s.Contains(v) // 1 allocation, v is moved to the heap
}
}
})
b.Run("Contains slice", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for i := range nums {
s.Contains(nums[i : i+1]...) // no allocations, using heap-allocated slice
}
}
})
b.Run("ContainsOne", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for _, v := range nums {
s.ContainsOne(v) // no allocations, using stack-allocated v
}
}
})
}
func BenchmarkContainsComparison1Unsafe(b *testing.B) {
benchContainsComparison(b, 1, NewThreadUnsafeSet[int]())
}
func BenchmarkContainsComparison1Safe(b *testing.B) {
benchContainsComparison(b, 1, NewSet[int]())
}
func BenchmarkContainsComparison10Unsafe(b *testing.B) {
benchContainsComparison(b, 10, NewThreadUnsafeSet[int]())
}
func BenchmarkContainsComparison10Safe(b *testing.B) {
benchContainsComparison(b, 10, NewSet[int]())
}
func BenchmarkContainsComparison100Unsafe(b *testing.B) {
benchContainsComparison(b, 100, NewThreadUnsafeSet[int]())
}
func BenchmarkContainsComparison100Safe(b *testing.B) {
benchContainsComparison(b, 100, NewSet[int]())
}
func benchEqual(b *testing.B, n int, s, t Set[int]) {
nums := nrand(n)
for _, v := range nums {
s.Add(v)
t.Add(v)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
s.Equal(t)
}
}
func BenchmarkEqual1Safe(b *testing.B) {
benchEqual(b, 1, NewSet[int](), NewSet[int]())
}
func BenchmarkEqual1Unsafe(b *testing.B) {
benchEqual(b, 1, NewThreadUnsafeSet[int](), NewThreadUnsafeSet[int]())
}
func BenchmarkEqual10Safe(b *testing.B) {
benchEqual(b, 10, NewSet[int](), NewSet[int]())
}
func BenchmarkEqual10Unsafe(b *testing.B) {
benchEqual(b, 10, NewThreadUnsafeSet[int](), NewThreadUnsafeSet[int]())
}
func BenchmarkEqual100Safe(b *testing.B) {
benchEqual(b, 100, NewSet[int](), NewSet[int]())
}
func BenchmarkEqual100Unsafe(b *testing.B) {
benchEqual(b, 100, NewThreadUnsafeSet[int](), NewThreadUnsafeSet[int]())
}
func benchDifference(b *testing.B, n int, s, t Set[int]) {
nums := nrand(n)
for _, v := range nums {
s.Add(v)
}
for _, v := range nums[:n/2] {
t.Add(v)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
s.Difference(t)
}
}
func benchIsSubset(b *testing.B, n int, s, t Set[int]) {
nums := nrand(n)
for _, v := range nums {
s.Add(v)
t.Add(v)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
s.IsSubset(t)
}
}
func BenchmarkIsSubset1Safe(b *testing.B) {
benchIsSubset(b, 1, NewSet[int](), NewSet[int]())
}
func BenchmarkIsSubset1Unsafe(b *testing.B) {
benchIsSubset(b, 1, NewThreadUnsafeSet[int](), NewThreadUnsafeSet[int]())
}
func BenchmarkIsSubset10Safe(b *testing.B) {
benchIsSubset(b, 10, NewSet[int](), NewSet[int]())
}
func BenchmarkIsSubset10Unsafe(b *testing.B) {
benchIsSubset(b, 10, NewThreadUnsafeSet[int](), NewThreadUnsafeSet[int]())
}
func BenchmarkIsSubset100Safe(b *testing.B) {
benchIsSubset(b, 100, NewSet[int](), NewSet[int]())
}
func BenchmarkIsSubset100Unsafe(b *testing.B) {
benchIsSubset(b, 100, NewThreadUnsafeSet[int](), NewThreadUnsafeSet[int]())
}
func benchIsSuperset(b *testing.B, n int, s, t Set[int]) {
nums := nrand(n)
for _, v := range nums {
s.Add(v)
t.Add(v)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
s.IsSuperset(t)
}
}
func BenchmarkIsSuperset1Safe(b *testing.B) {
benchIsSuperset(b, 1, NewSet[int](), NewSet[int]())
}
func BenchmarkIsSuperset1Unsafe(b *testing.B) {
benchIsSuperset(b, 1, NewThreadUnsafeSet[int](), NewThreadUnsafeSet[int]())
}
func BenchmarkIsSuperset10Safe(b *testing.B) {
benchIsSuperset(b, 10, NewSet[int](), NewSet[int]())
}
func BenchmarkIsSuperset10Unsafe(b *testing.B) {
benchIsSuperset(b, 10, NewThreadUnsafeSet[int](), NewThreadUnsafeSet[int]())
}
func BenchmarkIsSuperset100Safe(b *testing.B) {
benchIsSuperset(b, 100, NewSet[int](), NewSet[int]())
}
func BenchmarkIsSuperset100Unsafe(b *testing.B) {
benchIsSuperset(b, 100, NewThreadUnsafeSet[int](), NewThreadUnsafeSet[int]())
}
func benchIsProperSubset(b *testing.B, n int, s, t Set[int]) {
nums := nrand(n)
for _, v := range nums {
s.Add(v)
t.Add(v)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
s.IsProperSubset(t)
}
}
func BenchmarkIsProperSubset1Safe(b *testing.B) {
benchIsProperSubset(b, 1, NewSet[int](), NewSet[int]())
}
func BenchmarkIsProperSubset1Unsafe(b *testing.B) {
benchIsProperSubset(b, 1, NewThreadUnsafeSet[int](), NewThreadUnsafeSet[int]())
}
func BenchmarkIsProperSubset10Safe(b *testing.B) {
benchIsProperSubset(b, 10, NewSet[int](), NewSet[int]())
}
func BenchmarkIsProperSubset10Unsafe(b *testing.B) {
benchIsProperSubset(b, 10, NewThreadUnsafeSet[int](), NewThreadUnsafeSet[int]())
}
func BenchmarkIsProperSubset100Safe(b *testing.B) {
benchIsProperSubset(b, 100, NewSet[int](), NewSet[int]())
}
func BenchmarkIsProperSubset100Unsafe(b *testing.B) {
benchIsProperSubset(b, 100, NewThreadUnsafeSet[int](), NewThreadUnsafeSet[int]())
}
func benchIsProperSuperset(b *testing.B, n int, s, t Set[int]) {
nums := nrand(n)
for _, v := range nums {
s.Add(v)
t.Add(v)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
s.IsProperSuperset(t)
}
}
func BenchmarkIsProperSuperset1Safe(b *testing.B) {
benchIsProperSuperset(b, 1, NewSet[int](), NewSet[int]())
}
func BenchmarkIsProperSuperset1Unsafe(b *testing.B) {
benchIsProperSuperset(b, 1, NewThreadUnsafeSet[int](), NewThreadUnsafeSet[int]())
}
func BenchmarkIsProperSuperset10Safe(b *testing.B) {
benchIsProperSuperset(b, 10, NewSet[int](), NewSet[int]())
}
func BenchmarkIsProperSuperset10Unsafe(b *testing.B) {
benchIsProperSuperset(b, 10, NewThreadUnsafeSet[int](), NewThreadUnsafeSet[int]())
}
func BenchmarkIsProperSuperset100Safe(b *testing.B) {
benchIsProperSuperset(b, 100, NewSet[int](), NewSet[int]())
}
func BenchmarkIsProperSuperset100Unsafe(b *testing.B) {
benchIsProperSuperset(b, 100, NewThreadUnsafeSet[int](), NewThreadUnsafeSet[int]())
}
func BenchmarkDifference1Safe(b *testing.B) {
benchDifference(b, 1, NewSet[int](), NewSet[int]())
}
func BenchmarkDifference1Unsafe(b *testing.B) {
benchDifference(b, 1, NewThreadUnsafeSet[int](), NewThreadUnsafeSet[int]())
}
func BenchmarkDifference10Safe(b *testing.B) {
benchDifference(b, 10, NewSet[int](), NewSet[int]())
}
func BenchmarkDifference10Unsafe(b *testing.B) {
benchDifference(b, 10, NewThreadUnsafeSet[int](), NewThreadUnsafeSet[int]())
}
func BenchmarkDifference100Safe(b *testing.B) {
benchDifference(b, 100, NewSet[int](), NewSet[int]())
}
func BenchmarkDifference100Unsafe(b *testing.B) {
benchDifference(b, 100, NewThreadUnsafeSet[int](), NewThreadUnsafeSet[int]())
}
func benchIntersect(b *testing.B, n int, s, t Set[int]) {
nums := nrand(int(float64(n) * float64(1.5)))
for _, v := range nums[:n] {
s.Add(v)
}
for _, v := range nums[n/2:] {
t.Add(v)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
s.Intersect(t)
}
}
func BenchmarkIntersect1Safe(b *testing.B) {
benchIntersect(b, 1, NewSet[int](), NewSet[int]())
}
func BenchmarkIntersect1Unsafe(b *testing.B) {
benchIntersect(b, 1, NewThreadUnsafeSet[int](), NewThreadUnsafeSet[int]())
}
func BenchmarkIntersect10Safe(b *testing.B) {
benchIntersect(b, 10, NewSet[int](), NewSet[int]())
}
func BenchmarkIntersect10Unsafe(b *testing.B) {
benchIntersect(b, 10, NewThreadUnsafeSet[int](), NewThreadUnsafeSet[int]())
}
func BenchmarkIntersect100Safe(b *testing.B) {
benchIntersect(b, 100, NewSet[int](), NewSet[int]())
}
func BenchmarkIntersect100Unsafe(b *testing.B) {
benchIntersect(b, 100, NewThreadUnsafeSet[int](), NewThreadUnsafeSet[int]())
}
func benchSymmetricDifference(b *testing.B, n int, s, t Set[int]) {
nums := nrand(int(float64(n) * float64(1.5)))
for _, v := range nums[:n] {
s.Add(v)
}
for _, v := range nums[n/2:] {
t.Add(v)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
s.SymmetricDifference(t)
}
}
func BenchmarkSymmetricDifference1Safe(b *testing.B) {
benchSymmetricDifference(b, 1, NewSet[int](), NewSet[int]())
}
func BenchmarkSymmetricDifference1Unsafe(b *testing.B) {
benchSymmetricDifference(b, 1, NewThreadUnsafeSet[int](), NewThreadUnsafeSet[int]())
}
func BenchmarkSymmetricDifference10Safe(b *testing.B) {
benchSymmetricDifference(b, 10, NewSet[int](), NewSet[int]())
}
func BenchmarkSymmetricDifference10Unsafe(b *testing.B) {
benchSymmetricDifference(b, 10, NewThreadUnsafeSet[int](), NewThreadUnsafeSet[int]())
}
func BenchmarkSymmetricDifference100Safe(b *testing.B) {
benchSymmetricDifference(b, 100, NewSet[int](), NewSet[int]())
}
func BenchmarkSymmetricDifference100Unsafe(b *testing.B) {
benchSymmetricDifference(b, 100, NewThreadUnsafeSet[int](), NewThreadUnsafeSet[int]())
}
func benchUnion(b *testing.B, n int, s, t Set[int]) {
nums := nrand(n)
for _, v := range nums[:n/2] {
s.Add(v)
}
for _, v := range nums[n/2:] {
t.Add(v)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
s.Union(t)
}
}
func BenchmarkUnion1Safe(b *testing.B) {
benchUnion(b, 1, NewSet[int](), NewSet[int]())
}
func BenchmarkUnion1Unsafe(b *testing.B) {
benchUnion(b, 1, NewThreadUnsafeSet[int](), NewThreadUnsafeSet[int]())
}
func BenchmarkUnion10Safe(b *testing.B) {
benchUnion(b, 10, NewSet[int](), NewSet[int]())
}
func BenchmarkUnion10Unsafe(b *testing.B) {
benchUnion(b, 10, NewThreadUnsafeSet[int](), NewThreadUnsafeSet[int]())
}
func BenchmarkUnion100Safe(b *testing.B) {
benchUnion(b, 100, NewSet[int](), NewSet[int]())
}
func BenchmarkUnion100Unsafe(b *testing.B) {
benchUnion(b, 100, NewThreadUnsafeSet[int](), NewThreadUnsafeSet[int]())
}
func benchEach(b *testing.B, n int, s Set[int]) {
nums := nrand(n)
for _, v := range nums {
s.Add(v)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
s.Each(func(elem int) bool {
return false
})
}
}
func BenchmarkEach1Safe(b *testing.B) {
benchEach(b, 1, NewSet[int]())
}
func BenchmarkEach1Unsafe(b *testing.B) {
benchEach(b, 1, NewThreadUnsafeSet[int]())
}
func BenchmarkEach10Safe(b *testing.B) {
benchEach(b, 10, NewSet[int]())
}
func BenchmarkEach10Unsafe(b *testing.B) {
benchEach(b, 10, NewThreadUnsafeSet[int]())
}
func BenchmarkEach100Safe(b *testing.B) {
benchEach(b, 100, NewSet[int]())
}
func BenchmarkEach100Unsafe(b *testing.B) {
benchEach(b, 100, NewThreadUnsafeSet[int]())
}
func benchIter(b *testing.B, n int, s Set[int]) {
nums := nrand(n)
for _, v := range nums {
s.Add(v)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
c := s.Iter()
for range c {
}
}
}
func BenchmarkIter1Safe(b *testing.B) {
benchIter(b, 1, NewSet[int]())
}
func BenchmarkIter1Unsafe(b *testing.B) {
benchIter(b, 1, NewThreadUnsafeSet[int]())
}
func BenchmarkIter10Safe(b *testing.B) {
benchIter(b, 10, NewSet[int]())
}
func BenchmarkIter10Unsafe(b *testing.B) {
benchIter(b, 10, NewThreadUnsafeSet[int]())
}
func BenchmarkIter100Safe(b *testing.B) {
benchIter(b, 100, NewSet[int]())
}
func BenchmarkIter100Unsafe(b *testing.B) {
benchIter(b, 100, NewThreadUnsafeSet[int]())
}
func benchIterator(b *testing.B, n int, s Set[int]) {
nums := nrand(n)
for _, v := range nums {
s.Add(v)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
c := s.Iterator().C
for range c {
}
}
}
func BenchmarkIterator1Safe(b *testing.B) {
benchIterator(b, 1, NewSet[int]())
}
func BenchmarkIterator1Unsafe(b *testing.B) {
benchIterator(b, 1, NewThreadUnsafeSet[int]())
}
func BenchmarkIterator10Safe(b *testing.B) {
benchIterator(b, 10, NewSet[int]())
}
func BenchmarkIterator10Unsafe(b *testing.B) {
benchIterator(b, 10, NewThreadUnsafeSet[int]())
}
func BenchmarkIterator100Safe(b *testing.B) {
benchIterator(b, 100, NewSet[int]())
}
func BenchmarkIterator100Unsafe(b *testing.B) {
benchIterator(b, 100, NewThreadUnsafeSet[int]())
}
func benchString(b *testing.B, n int, s Set[int]) {
nums := nrand(n)
for _, v := range nums {
s.Add(v)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = s.String()
}
}
func BenchmarkString1Safe(b *testing.B) {
benchString(b, 1, NewSet[int]())
}
func BenchmarkString1Unsafe(b *testing.B) {
benchString(b, 1, NewThreadUnsafeSet[int]())
}
func BenchmarkString10Safe(b *testing.B) {
benchString(b, 10, NewSet[int]())
}
func BenchmarkString10Unsafe(b *testing.B) {
benchString(b, 10, NewThreadUnsafeSet[int]())
}
func BenchmarkString100Safe(b *testing.B) {
benchString(b, 100, NewSet[int]())
}
func BenchmarkString100Unsafe(b *testing.B) {
benchString(b, 100, NewThreadUnsafeSet[int]())
}
func benchToSlice(b *testing.B, s Set[int]) {
nums := nrand(b.N)
for _, v := range nums {
s.Add(v)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
s.ToSlice()
}
}
func BenchmarkToSliceSafe(b *testing.B) {
benchToSlice(b, NewSet[int]())
}
func BenchmarkToSliceUnsafe(b *testing.B) {
benchToSlice(b, NewThreadUnsafeSet[int]())
}