@@ -2,43 +2,56 @@ package cmp_test
2
2
3
3
import (
4
4
"cmp"
5
- "slices"
6
- "strconv"
5
+ "math"
7
6
"testing"
8
7
)
9
8
9
+ var sum int
10
+
10
11
func BenchmarkCompare_int (b * testing.B ) {
11
- var lst [1_000_000 ]int
12
- for i := range lst {
13
- lst [i ] = i
12
+ lst := [... ]int {
13
+ 0xfa3 , 0x7fe , 0x03c , 0xcb9 ,
14
+ 0x4ce , 0x4fb , 0x7d5 , 0x38f ,
15
+ 0x73b , 0x322 , 0x85c , 0xf4d ,
16
+ 0xbbc , 0x032 , 0x059 , 0xb93 ,
14
17
}
15
- b .ResetTimer ()
16
-
17
18
for n := 0 ; n < b .N ; n ++ {
18
- slices . SortFunc (lst [: ], cmp . Compare )
19
+ sum += cmp . Compare (lst [n % len ( lst ) ], lst [( 2 * n ) % len ( lst )] )
19
20
}
20
21
}
21
22
22
23
func BenchmarkCompare_float64 (b * testing.B ) {
23
- var lst [1_000_000 ]float64
24
- for i := range lst {
25
- lst [i ] = float64 (i )
24
+ lst := [... ]float64 {
25
+ 0.35573281 , 0.77552566 , 0.19006500 , 0.66436280 ,
26
+ 0.02769279 , 0.97572397 , 0.40945068 , 0.26422857 ,
27
+ 0.10985792 , 0.35659522 , 0.82752613 , 0.18875522 ,
28
+ 0.16410543 , 0.03578153 , 0.51636871 , math .NaN (),
26
29
}
27
- b .ResetTimer ()
28
-
29
30
for n := 0 ; n < b .N ; n ++ {
30
- slices . SortFunc (lst [: ], cmp . Compare )
31
+ sum += cmp . Compare (lst [n % len ( lst ) ], lst [( 2 * n ) % len ( lst )] )
31
32
}
32
33
}
33
34
34
- func BenchmarkCompare_string (b * testing.B ) {
35
- var lst [1_000_000 ]string
36
- for i := range lst {
37
- lst [i ] = strconv .Itoa (i )
35
+ func BenchmarkCompare_strings (b * testing.B ) {
36
+ lst := [... ]string {
37
+ "time" ,
38
+ "person" ,
39
+ "year" ,
40
+ "way" ,
41
+ "day" ,
42
+ "thing" ,
43
+ "man" ,
44
+ "world" ,
45
+ "life" ,
46
+ "hand" ,
47
+ "part" ,
48
+ "child" ,
49
+ "eye" ,
50
+ "woman" ,
51
+ "place" ,
52
+ "work" ,
38
53
}
39
- b .ResetTimer ()
40
-
41
54
for n := 0 ; n < b .N ; n ++ {
42
- slices . SortFunc (lst [: ], cmp . Compare )
55
+ sum += cmp . Compare (lst [n % len ( lst ) ], lst [( 2 * n ) % len ( lst )] )
43
56
}
44
57
}
0 commit comments