Skip to content

Commit 5896093

Browse files
committed
add comments for each package
1 parent 5c7d615 commit 5896093

File tree

11 files changed

+123
-52
lines changed

11 files changed

+123
-52
lines changed

README.md

+88-52
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Go Benchmarks
22

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)
44
*Gopher drawings by [Renee French](http://reneefrench.blogspot.com)*
55

66
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
4141
### base64
4242

4343
```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.
4447
package base64
4548

4649
import (
@@ -84,15 +87,18 @@ func BenchmarkBase64regex(b *testing.B) {
8487
$ go test -bench . -benchmem
8588
goos: darwin
8689
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
8992
PASS
90-
ok _/Users/simonwaldherr/git/golang-benchmarks/base64 3.790s
93+
ok _/Users/simonwaldherr/git/golang-benchmarks/base64 4.435s
9194
```
9295

9396
### between
9497

9598
```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.
96102
package between
97103

98104
import (
@@ -176,17 +182,22 @@ func BenchmarkFulltextParse(b *testing.B) {
176182
$ go test -bench . -benchmem
177183
goos: darwin
178184
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
183189
PASS
184-
ok _/Users/simonwaldherr/git/golang-benchmarks/between 6.035s
190+
ok _/Users/simonwaldherr/git/golang-benchmarks/between 7.373s
185191
```
186192

187193
### concat
188194

189195
```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.
190201
package concat
191202

192203
import (
@@ -222,16 +233,18 @@ func BenchmarkConcatBuilder(b *testing.B) {
222233
$ go test -bench . -benchmem
223234
goos: darwin
224235
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
228239
PASS
229-
ok _/Users/simonwaldherr/git/golang-benchmarks/concat 45.930s
240+
ok _/Users/simonwaldherr/git/golang-benchmarks/concat 57.403s
230241
```
231242

232243
### contains
233244

234245
```go
246+
// Package contains tests various ways of checking
247+
// if a string is contained in another string.
235248
package contains
236249

237250
import (
@@ -374,21 +387,22 @@ func BenchmarkMatchNot(b *testing.B) {
374387
$ go test -bench . -benchmem
375388
goos: darwin
376389
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
385398
PASS
386-
ok _/Users/simonwaldherr/git/golang-benchmarks/contains 12.069s
399+
ok _/Users/simonwaldherr/git/golang-benchmarks/contains 10.863s
387400
```
388401

389402
### foreach
390403

391404
```go
405+
// Package foreach benchmarks ranging over slices and maps.
392406
package foreach
393407

394408
import (
@@ -469,17 +483,20 @@ func BenchmarkRangeSliceKey(b *testing.B) {
469483
$ go test -bench . -benchmem
470484
goos: darwin
471485
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
476490
PASS
477-
ok _/Users/simonwaldherr/git/golang-benchmarks/foreach 7.600s
491+
ok _/Users/simonwaldherr/git/golang-benchmarks/foreach 7.381s
478492
```
479493

480494
### hash
481495

482496
```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.
483500
package hash
484501

485502
import (
@@ -553,23 +570,24 @@ func BenchmarkWhirlpool(b *testing.B) {
553570
$ go test -bench . -benchmem
554571
goos: darwin
555572
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
566583
PASS
567-
ok _/Users/simonwaldherr/git/golang-benchmarks/hash 16.497s
584+
ok _/Users/simonwaldherr/git/golang-benchmarks/hash 19.269s
568585
```
569586

570587
### index
571588

572589
```go
590+
// Package index benchmarks access on maps with various data types as keys.
573591
package index
574592

575593
import (
@@ -646,15 +664,16 @@ func BenchmarkMapIntKeys(b *testing.B) {
646664
$ go test -bench . -benchmem
647665
goos: darwin
648666
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
651669
PASS
652-
ok _/Users/simonwaldherr/git/golang-benchmarks/index 6.454s
670+
ok _/Users/simonwaldherr/git/golang-benchmarks/index 6.281s
653671
```
654672

655673
### parse
656674

657675
```go
676+
// Package parse benchmarks parsing.
658677
package parse
659678

660679
import (
@@ -694,16 +713,19 @@ func BenchmarkParseFloat(b *testing.B) {
694713
$ go test -bench . -benchmem
695714
goos: darwin
696715
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
700719
PASS
701-
ok _/Users/simonwaldherr/git/golang-benchmarks/parse 6.414s
720+
ok _/Users/simonwaldherr/git/golang-benchmarks/parse 6.911s
702721
```
703722

704723
### random
705724

706725
```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.
707729
package random
708730

709731
import (
@@ -758,21 +780,25 @@ func GenerateRandomString(s int) (string, error) {
758780
$ go test -bench . -benchmem
759781
goos: darwin
760782
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
764786
PASS
765-
ok _/Users/simonwaldherr/git/golang-benchmarks/random 5.209s
787+
ok _/Users/simonwaldherr/git/golang-benchmarks/random 5.534s
766788
```
767789

768790
### regexp
769791

770792
```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
771795
package regexp
772796

773797
import (
774798
"regexp"
775799
"testing"
800+
801+
"simonwaldherr.de/go/golibs/regex"
776802
)
777803

778804
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) {
797823
r.MatchString("[email protected]")
798824
}
799825
}
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+
}
800835
```
801836

802837
```
803838
$ go test -bench . -benchmem
804839
goos: darwin
805840
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
808844
PASS
809-
ok _/Users/simonwaldherr/git/golang-benchmarks/regexp 3.632s
845+
ok _/Users/simonwaldherr/git/golang-benchmarks/regexp 6.832s
810846
```
811847

base64/base64_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Package base64 benchmarks some base64 functions.
2+
// On all tested systems it's faster to decode a
3+
// base64 encoded string instead of a check via regex.
14
package base64
25

36
import (

between/between_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Package between compares the performance of checking
2+
// if a number is between to other numbers via regex
3+
// and by parsing the number as integer.
14
package between
25

36
import (

concat/concat_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
// Package concat benchmarks the performance of
2+
// various string concatination methods.
3+
// Instead of just concat a string to another string
4+
// it is also possible (and much faster) to use
5+
// a bytes buffer.
16
package concat
27

38
import (

contains/contains_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Package contains tests various ways of checking
2+
// if a string is contained in another string.
13
package contains
24

35
import (

foreach/foreach_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package foreach benchmarks ranging over slices and maps.
12
package foreach
23

34
import (

hash/hash_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Package hash benchmarks various hashing algorithms.
2+
// Especially with hashing algorithms, faster is not always better.
3+
// One should always decide on the basis of the respective requirements.
14
package hash
25

36
import (

index/index_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package index benchmarks access on maps with various data types as keys.
12
package index
23

34
import (

parse/parse_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package parse benchmarks parsing.
12
package parse
23

34
import (

random/random_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Package random compares math/rand with crypto/rand.
2+
// math/rand is much faster than crypto/rand, but it
3+
// returns only a pseudo random number.
14
package random
25

36
import (

regexp/regexp_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
// Package regexp benchmarks the performance of a pre-compiled regexp match
2+
// a non-pre-compiled match and JIT-cached-compilation via golibs: https://simonwaldherr.de/go/golibs
13
package regexp
24

35
import (
46
"regexp"
57
"testing"
8+
9+
"simonwaldherr.de/go/golibs/regex"
610
)
711

812
var regexpStr string = `^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,9}$`
@@ -27,3 +31,12 @@ func BenchmarkMatchStringCompiled(b *testing.B) {
2731
r.MatchString("[email protected]")
2832
}
2933
}
34+
35+
func BenchmarkMatchStringGolibs(b *testing.B) {
36+
for n := 0; n < b.N; n++ {
37+
_, err := regex.MatchString("[email protected]", regexpStr)
38+
if err != nil {
39+
panic(err)
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)