1
1
# Go Benchmarks
2
2
3
- ![ Gopher Racing] ( https://raw.githubusercontent.com/SimonWaldherr/golang-benchmarks/master/_gophers_race.jpg " Gopher Racing - by Renee French ")
3
+ [ ![ Gopher Racing] ( https://raw.githubusercontent.com/SimonWaldherr/golang-benchmarks/master/_gophers_race.jpg " Gopher Racing - by Renee French ")] ( https://github.com/SimonWaldherr/golang-benchmarks )
4
4
* Gopher drawings by [ Renee French] ( http://reneefrench.blogspot.com ) *
5
5
6
6
In programming in general, and in Golang in particular, many roads lead to Rome.
@@ -41,6 +41,9 @@ If you\'re interested in new programming languages, you should definitely take a
41
41
### base64
42
42
43
43
``` go
44
+ // Package base64 benchmarks some base64 functions.
45
+ // On all tested systems it's faster to decode a
46
+ // base64 encoded string instead of a check via regex.
44
47
package base64
45
48
46
49
import (
@@ -84,15 +87,18 @@ func BenchmarkBase64regex(b *testing.B) {
84
87
$ go test -bench . -benchmem
85
88
goos: darwin
86
89
goarch: amd64
87
- BenchmarkBase64decode-8 10000000 165 ns/op 40 B/op 3 allocs/op
88
- BenchmarkBase64regex-8 50000 31337 ns/op 98160 B/op 218 allocs/op
90
+ BenchmarkBase64decode-8 10000000 192 ns/op 40 B/op 3 allocs/op
91
+ BenchmarkBase64regex-8 50000 38991 ns/op 98160 B/op 218 allocs/op
89
92
PASS
90
- ok _/Users/simonwaldherr/git/golang-benchmarks/base64 3.790s
93
+ ok _/Users/simonwaldherr/git/golang-benchmarks/base64 4.435s
91
94
```
92
95
93
96
### between
94
97
95
98
``` go
99
+ // Package between compares the performance of checking
100
+ // if a number is between to other numbers via regex
101
+ // and by parsing the number as integer.
96
102
package between
97
103
98
104
import (
@@ -176,17 +182,22 @@ func BenchmarkFulltextParse(b *testing.B) {
176
182
$ go test -bench . -benchmem
177
183
goos: darwin
178
184
goarch: amd64
179
- BenchmarkNumberRegEx-8 50000 23462 ns/op 92176 B/op 162 allocs/op
180
- BenchmarkFulltextRegEx-8 100000 19682 ns/op 87680 B/op 124 allocs/op
181
- BenchmarkNumberParse-8 20000000 57.0 ns/op 0 B/op 0 allocs/op
182
- BenchmarkFulltextParse-8 1000000 1130 ns/op 32 B/op 2 allocs/op
185
+ BenchmarkNumberRegEx-8 50000 25743 ns/op 92176 B/op 162 allocs/op
186
+ BenchmarkFulltextRegEx-8 100000 24219 ns/op 87680 B/op 124 allocs/op
187
+ BenchmarkNumberParse-8 30000000 58.5 ns/op 0 B/op 0 allocs/op
188
+ BenchmarkFulltextParse-8 1000000 1291 ns/op 32 B/op 2 allocs/op
183
189
PASS
184
- ok _/Users/simonwaldherr/git/golang-benchmarks/between 6.035s
190
+ ok _/Users/simonwaldherr/git/golang-benchmarks/between 7.373s
185
191
```
186
192
187
193
### concat
188
194
189
195
``` go
196
+ // Package concat benchmarks the performance of
197
+ // various string concatination methods.
198
+ // Instead of just concat a string to another string
199
+ // it is also possible (and much faster) to use
200
+ // a bytes buffer.
190
201
package concat
191
202
192
203
import (
@@ -222,16 +233,18 @@ func BenchmarkConcatBuilder(b *testing.B) {
222
233
$ go test -bench . -benchmem
223
234
goos: darwin
224
235
goarch: amd64
225
- BenchmarkConcatString-8 1000000 40091 ns/op 503992 B/op 1 allocs/op
226
- BenchmarkConcatBuffer-8 200000000 7.41 ns/op 2 B/op 0 allocs/op
227
- BenchmarkConcatBuilder-8 1000000000 3.13 ns/op 6 B/op 0 allocs/op
236
+ BenchmarkConcatString-8 1000000 50605 ns/op 503992 B/op 1 allocs/op
237
+ BenchmarkConcatBuffer-8 200000000 7.95 ns/op 2 B/op 0 allocs/op
238
+ BenchmarkConcatBuilder-8 1000000000 3.97 ns/op 6 B/op 0 allocs/op
228
239
PASS
229
- ok _/Users/simonwaldherr/git/golang-benchmarks/concat 45.930s
240
+ ok _/Users/simonwaldherr/git/golang-benchmarks/concat 57.403s
230
241
```
231
242
232
243
### contains
233
244
234
245
``` go
246
+ // Package contains tests various ways of checking
247
+ // if a string is contained in another string.
235
248
package contains
236
249
237
250
import (
@@ -374,21 +387,22 @@ func BenchmarkMatchNot(b *testing.B) {
374
387
$ go test -bench . -benchmem
375
388
goos: darwin
376
389
goarch: amd64
377
- BenchmarkContains-8 100000000 14.7 ns/op 0 B/op 0 allocs/op
378
- BenchmarkContainsNot-8 100000000 13.8 ns/op 0 B/op 0 allocs/op
379
- BenchmarkContainsBytes-8 50000000 23.9 ns/op 0 B/op 0 allocs/op
380
- BenchmarkContainsBytesNot-8 50000000 27.5 ns/op 0 B/op 0 allocs/op
381
- BenchmarkCompileMatch-8 10000000 122 ns/op 0 B/op 0 allocs/op
382
- BenchmarkCompileMatchNot-8 20000000 69.9 ns/op 0 B/op 0 allocs/op
383
- BenchmarkMatch-8 200000 6561 ns/op 38912 B/op 27 allocs/op
384
- BenchmarkMatchNot-8 300000 7574 ns/op 38912 B/op 27 allocs/op
390
+ BenchmarkContains-8 100000000 13.6 ns/op 0 B/op 0 allocs/op
391
+ BenchmarkContainsNot-8 100000000 13.9 ns/op 0 B/op 0 allocs/op
392
+ BenchmarkContainsBytes-8 50000000 24.2 ns/op 0 B/op 0 allocs/op
393
+ BenchmarkContainsBytesNot-8 50000000 25.2 ns/op 0 B/op 0 allocs/op
394
+ BenchmarkCompileMatch-8 10000000 137 ns/op 0 B/op 0 allocs/op
395
+ BenchmarkCompileMatchNot-8 20000000 73.2 ns/op 0 B/op 0 allocs/op
396
+ BenchmarkMatch-8 200000 6403 ns/op 38912 B/op 27 allocs/op
397
+ BenchmarkMatchNot-8 200000 5425 ns/op 38912 B/op 27 allocs/op
385
398
PASS
386
- ok _/Users/simonwaldherr/git/golang-benchmarks/contains 12.069s
399
+ ok _/Users/simonwaldherr/git/golang-benchmarks/contains 10.863s
387
400
```
388
401
389
402
### foreach
390
403
391
404
``` go
405
+ // Package foreach benchmarks ranging over slices and maps.
392
406
package foreach
393
407
394
408
import (
@@ -469,17 +483,20 @@ func BenchmarkRangeSliceKey(b *testing.B) {
469
483
$ go test -bench . -benchmem
470
484
goos: darwin
471
485
goarch: amd64
472
- BenchmarkForMap-8 50000000 27.0 ns/op 0 B/op 0 allocs/op
473
- BenchmarkRangeMap-8 20000000 76.2 ns/op 0 B/op 0 allocs/op
474
- BenchmarkRangeSlice-8 500000000 3.73 ns/op 0 B/op 0 allocs/op
475
- BenchmarkRangeSliceKey-8 500000000 3.90 ns/op 0 B/op 0 allocs/op
486
+ BenchmarkForMap-8 50000000 33.9 ns/op 0 B/op 0 allocs/op
487
+ BenchmarkRangeMap-8 20000000 80.6 ns/op 0 B/op 0 allocs/op
488
+ BenchmarkRangeSlice-8 500000000 3.79 ns/op 0 B/op 0 allocs/op
489
+ BenchmarkRangeSliceKey-8 300000000 4.09 ns/op 0 B/op 0 allocs/op
476
490
PASS
477
- ok _/Users/simonwaldherr/git/golang-benchmarks/foreach 7.600s
491
+ ok _/Users/simonwaldherr/git/golang-benchmarks/foreach 7.381s
478
492
```
479
493
480
494
### hash
481
495
482
496
``` go
497
+ // Package hash benchmarks various hashing algorithms.
498
+ // Especially with hashing algorithms, faster is not always better.
499
+ // One should always decide on the basis of the respective requirements.
483
500
package hash
484
501
485
502
import (
@@ -553,23 +570,24 @@ func BenchmarkWhirlpool(b *testing.B) {
553
570
$ go test -bench . -benchmem
554
571
goos: darwin
555
572
goarch: amd64
556
- BenchmarkAdler32-8 1000000 1052 ns/op 8 B/op 1 allocs/op
557
- BenchmarkCRC32-8 10000000 193 ns/op 8 B/op 1 allocs/op
558
- BenchmarkFnv128-8 200000 9713 ns/op 16 B/op 1 allocs/op
559
- BenchmarkMD5-8 500000 3420 ns/op 16 B/op 1 allocs/op
560
- BenchmarkSHA1-8 500000 2530 ns/op 32 B/op 1 allocs/op
561
- BenchmarkSHA256-8 200000 5773 ns/op 32 B/op 1 allocs/op
562
- BenchmarkSHA512-8 300000 4619 ns/op 64 B/op 1 allocs/op
563
- BenchmarkSHA3256-8 200000 8197 ns/op 512 B/op 3 allocs/op
564
- BenchmarkSHA3512-8 100000 14362 ns/op 576 B/op 3 allocs/op
565
- BenchmarkWhirlpool-8 20000 70905 ns/op 64 B/op 1 allocs/op
573
+ BenchmarkAdler32-8 2000000 1139 ns/op 8 B/op 1 allocs/op
574
+ BenchmarkCRC32-8 10000000 184 ns/op 8 B/op 1 allocs/op
575
+ BenchmarkFnv128-8 200000 9386 ns/op 16 B/op 1 allocs/op
576
+ BenchmarkMD5-8 500000 3442 ns/op 16 B/op 1 allocs/op
577
+ BenchmarkSHA1-8 500000 2693 ns/op 32 B/op 1 allocs/op
578
+ BenchmarkSHA256-8 300000 6123 ns/op 32 B/op 1 allocs/op
579
+ BenchmarkSHA512-8 300000 4669 ns/op 64 B/op 1 allocs/op
580
+ BenchmarkSHA3256-8 200000 8541 ns/op 512 B/op 3 allocs/op
581
+ BenchmarkSHA3512-8 100000 15178 ns/op 576 B/op 3 allocs/op
582
+ BenchmarkWhirlpool-8 20000 65768 ns/op 64 B/op 1 allocs/op
566
583
PASS
567
- ok _/Users/simonwaldherr/git/golang-benchmarks/hash 16.497s
584
+ ok _/Users/simonwaldherr/git/golang-benchmarks/hash 19.269s
568
585
```
569
586
570
587
### index
571
588
572
589
``` go
590
+ // Package index benchmarks access on maps with various data types as keys.
573
591
package index
574
592
575
593
import (
@@ -646,15 +664,16 @@ func BenchmarkMapIntKeys(b *testing.B) {
646
664
$ go test -bench . -benchmem
647
665
goos: darwin
648
666
goarch: amd64
649
- BenchmarkMapStringKeys-8 20000000 117 ns/op 0 B/op 0 allocs/op
650
- BenchmarkMapIntKeys-8 20000000 84.2 ns/op 0 B/op 0 allocs/op
667
+ BenchmarkMapStringKeys-8 20000000 130 ns/op 0 B/op 0 allocs/op
668
+ BenchmarkMapIntKeys-8 20000000 76.0 ns/op 0 B/op 0 allocs/op
651
669
PASS
652
- ok _/Users/simonwaldherr/git/golang-benchmarks/index 6.454s
670
+ ok _/Users/simonwaldherr/git/golang-benchmarks/index 6.281s
653
671
```
654
672
655
673
### parse
656
674
657
675
``` go
676
+ // Package parse benchmarks parsing.
658
677
package parse
659
678
660
679
import (
@@ -694,16 +713,19 @@ func BenchmarkParseFloat(b *testing.B) {
694
713
$ go test -bench . -benchmem
695
714
goos: darwin
696
715
goarch: amd64
697
- BenchmarkParseBool-8 500000000 3.25 ns/op 0 B/op 0 allocs/op
698
- BenchmarkParseInt-8 100000000 13.6 ns/op 0 B/op 0 allocs/op
699
- BenchmarkParseFloat-8 20000000 97.2 ns/op 0 B/op 0 allocs/op
716
+ BenchmarkParseBool-8 500000000 4.40 ns/op 0 B/op 0 allocs/op
717
+ BenchmarkParseInt-8 100000000 17.2 ns/op 0 B/op 0 allocs/op
718
+ BenchmarkParseFloat-8 20000000 122 ns/op 0 B/op 0 allocs/op
700
719
PASS
701
- ok _/Users/simonwaldherr/git/golang-benchmarks/parse 6.414s
720
+ ok _/Users/simonwaldherr/git/golang-benchmarks/parse 6.911s
702
721
```
703
722
704
723
### random
705
724
706
725
``` go
726
+ // Package random compares math/rand with crypto/rand.
727
+ // math/rand is much faster than crypto/rand, but it
728
+ // returns only a pseudo random number.
707
729
package random
708
730
709
731
import (
@@ -758,21 +780,25 @@ func GenerateRandomString(s int) (string, error) {
758
780
$ go test -bench . -benchmem
759
781
goos: darwin
760
782
goarch: amd64
761
- BenchmarkMathRand-8 30000000 34.9 ns/op 0 B/op 0 allocs/op
762
- BenchmarkCryptoRand-8 5000000 324 ns/op 162 B/op 5 allocs/op
763
- BenchmarkCryptoRandString-8 10000000 192 ns/op 128 B/op 3 allocs/op
783
+ BenchmarkMathRand-8 30000000 45.0 ns/op 0 B/op 0 allocs/op
784
+ BenchmarkCryptoRand-8 3000000 404 ns/op 162 B/op 5 allocs/op
785
+ BenchmarkCryptoRandString-8 10000000 225 ns/op 128 B/op 3 allocs/op
764
786
PASS
765
- ok _/Users/simonwaldherr/git/golang-benchmarks/random 5.209s
787
+ ok _/Users/simonwaldherr/git/golang-benchmarks/random 5.534s
766
788
```
767
789
768
790
### regexp
769
791
770
792
``` go
793
+ // Package regexp benchmarks the performance of a pre-compiled regexp match
794
+ // a non-pre-compiled match and JIT-cached-compilation via golibs: https://simonwaldherr.de/go/golibs
771
795
package regexp
772
796
773
797
import (
774
798
" regexp"
775
799
" testing"
800
+
801
+ " simonwaldherr.de/go/golibs/regex"
776
802
)
777
803
778
804
var regexpStr string = ` ^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,9}$`
@@ -797,15 +823,25 @@ func BenchmarkMatchStringCompiled(b *testing.B) {
797
823
r.
MatchString (
" [email protected] " )
798
824
}
799
825
}
826
+
827
+ func BenchmarkMatchStringGolibs (b *testing .B ) {
828
+ for n := 0 ; n < b.N ; n++ {
829
+ _ ,
err := regex.
MatchString (
" [email protected] " , regexpStr)
830
+ if err != nil {
831
+ panic (err)
832
+ }
833
+ }
834
+ }
800
835
```
801
836
802
837
```
803
838
$ go test -bench . -benchmem
804
839
goos: darwin
805
840
goarch: amd64
806
- BenchmarkMatchString-8 100000 13764 ns/op 48224 B/op 96 allocs/op
807
- BenchmarkMatchStringCompiled-8 2000000 689 ns/op 0 B/op 0 allocs/op
841
+ BenchmarkMatchString-8 100000 19799 ns/op 48224 B/op 96 allocs/op
842
+ BenchmarkMatchStringCompiled-8 2000000 755 ns/op 0 B/op 0 allocs/op
843
+ BenchmarkMatchStringGolibs-8 2000000 767 ns/op 0 B/op 0 allocs/op
808
844
PASS
809
- ok _/Users/simonwaldherr/git/golang-benchmarks/regexp 3.632s
845
+ ok _/Users/simonwaldherr/git/golang-benchmarks/regexp 6.832s
810
846
```
811
847
0 commit comments