forked from benbjohnson/immutable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenhanced_benchmarks_test.go
More file actions
109 lines (94 loc) · 1.97 KB
/
Copy pathenhanced_benchmarks_test.go
File metadata and controls
109 lines (94 loc) · 1.97 KB
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
package immutable
import (
"cmp"
"testing"
)
// Enhanced benchmarks using Go 1.24+ testing.B.Loop
func BenchmarkEnhanced_MapSet_Loop(b *testing.B) {
m := NewMap[int, string](nil)
for b.Loop() {
m = m.Set(b.N, "value")
}
}
func BenchmarkEnhanced_ListAppend_Loop(b *testing.B) {
l := NewList[int]()
for b.Loop() {
l = l.Append(b.N)
}
}
// Benchmark using built-in min/max vs custom functions
func BenchmarkBuiltin_MinMax(b *testing.B) {
values := []int{1, 5, 3, 9, 2, 7, 4, 8, 6}
b.Run("BuiltIn", func(b *testing.B) {
for b.Loop() {
_ = min(values[0], values[1], values[2])
_ = max(values[3], values[4], values[5])
}
})
b.Run("Custom", func(b *testing.B) {
for b.Loop() {
_ = customMin(values[0], values[1], values[2])
_ = customMax(values[3], values[4], values[5])
}
})
}
// Custom implementations for comparison
func customMin[T cmp.Ordered](a, b, c T) T {
if a <= b && a <= c {
return a
}
if b <= c {
return b
}
return c
}
func customMax[T cmp.Ordered](a, b, c T) T {
if a >= b && a >= c {
return a
}
if b >= c {
return b
}
return c
}
// Benchmark comparing cmp.Compare vs custom comparison
func BenchmarkCmp_Compare(b *testing.B) {
values := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
b.Run("CmpCompare", func(b *testing.B) {
for b.Loop() {
for i := 0; i < len(values)-1; i++ {
_ = cmp.Compare(values[i], values[i+1])
}
}
})
b.Run("CustomCompare", func(b *testing.B) {
for b.Loop() {
for i := 0; i < len(values)-1; i++ {
_ = defaultCompare(values[i], values[i+1])
}
}
})
}
// Benchmark built-in clear vs manual clearing
func BenchmarkBuiltin_Clear(b *testing.B) {
b.Run("BuiltInClear", func(b *testing.B) {
for b.Loop() {
slice := make([]int, 1000)
for i := range slice {
slice[i] = i
}
clear(slice)
}
})
b.Run("ManualClear", func(b *testing.B) {
for b.Loop() {
slice := make([]int, 1000)
for i := range slice {
slice[i] = i
}
for i := range slice {
slice[i] = 0
}
}
})
}