github.com/m4gshm/gollections@v0.0.13-0.20240331203319-a34a86e58a24/internal/benchmark/loop/over_vs_loop/over_test.go (about) 1 //go:build goexperiment.rangefunc 2 3 package over_vs_loop 4 5 import ( 6 "strconv" 7 "testing" 8 9 "github.com/m4gshm/gollections/loop" 10 "github.com/m4gshm/gollections/loop/range_" 11 "github.com/m4gshm/gollections/over" 12 ) 13 14 var max = 100000 15 16 var resultStr = "" 17 18 func Benchmark_loop_Converted(b *testing.B) { 19 integers := range_.Of(0, max) 20 for i := 0; i < b.N; i++ { 21 loop.Convert(integers, strconv.Itoa).ForEach(func(element string) { 22 resultStr = element 23 }) 24 } 25 } 26 27 func Benchmark_loop_Converted_All(b *testing.B) { 28 integers := range_.Of(0, max) 29 for i := 0; i < b.N; i++ { 30 for element := range loop.Convert(integers, strconv.Itoa).All { 31 resultStr = element 32 } 33 } 34 } 35 36 func Benchmark_over_Converted(b *testing.B) { 37 integers := range_.Of(0, max) 38 for i := 0; i < b.N; i++ { 39 for element := range over.Converted(integers.All, strconv.Itoa) { 40 resultStr = element 41 } 42 } 43 } 44 45 func Benchmark_over_Converted_direct(b *testing.B) { 46 integers := range_.Of(0, max) 47 for i := 0; i < b.N; i++ { 48 over.Converted(integers.All, strconv.Itoa)(func(element string) bool { 49 resultStr = element 50 return true 51 }) 52 } 53 } 54 55 func even(i int) bool { 56 return i%2 == 0 57 } 58 59 func Benchmark_loop_Convert_Filtered(b *testing.B) { 60 integers := range_.Of(0, max) 61 for i := 0; i < b.N; i++ { 62 loop.Convert(loop.Filter(integers, even), strconv.Itoa).ForEach(func(element string) { 63 resultStr = element 64 }) 65 } 66 } 67 68 func Benchmark_loop_Convert_Filtered_rangefunc(b *testing.B) { 69 integers := range_.Of(0, max) 70 for i := 0; i < b.N; i++ { 71 for element := range loop.Convert(loop.Filter(integers, even), strconv.Itoa).All { 72 resultStr = element 73 } 74 } 75 } 76 77 func Benchmark_over_Convert_Filtered(b *testing.B) { 78 integers := range_.Of(0, max) 79 for i := 0; i < b.N; i++ { 80 for element := range over.Converted(over.Filtered(integers.All, even), strconv.Itoa) { 81 resultStr = element 82 } 83 } 84 }