@@ -213,6 +213,163 @@ PASS
213
213
ok _/Users/simonwaldherr/git/golang-benchmarks/concat 45.930s
214
214
```
215
215
216
+ ### contains
217
+
218
+ ``` go
219
+ package contains
220
+
221
+ import (
222
+ " bytes"
223
+ " regexp"
224
+ " strings"
225
+ " testing"
226
+ )
227
+
228
+ // strings.Contains
229
+ func contains () bool {
230
+ return strings.Contains (" Lorem Ipsum" , " em Ip" )
231
+ }
232
+
233
+ func containsNot () bool {
234
+ return strings.Contains (" Lorem Ipsum" , " Dolor" )
235
+ }
236
+
237
+ func TestContains (t *testing .T ) {
238
+ if contains () == false {
239
+ t.Error (" ERROR: contains" )
240
+ }
241
+ if containsNot () == true {
242
+ t.Error (" ERROR: contains not" )
243
+ }
244
+ }
245
+
246
+ func BenchmarkContains (b *testing .B ) {
247
+ for n := 0 ; n < b.N ; n++ {
248
+ contains ()
249
+ }
250
+ }
251
+
252
+ func BenchmarkContainsNot (b *testing .B ) {
253
+ for n := 0 ; n < b.N ; n++ {
254
+ containsNot ()
255
+ }
256
+ }
257
+
258
+ // bytes.Contains
259
+ func containsBytes () bool {
260
+ return bytes.Contains ([]byte (" Lorem Ipsum" ), []byte (" em Ip" ))
261
+ }
262
+
263
+ func containsBytesNot () bool {
264
+ return bytes.Contains ([]byte (" Lorem Ipsum" ), []byte (" Dolor" ))
265
+ }
266
+
267
+ func TestContainsBytes (t *testing .T ) {
268
+ if containsBytes () == false {
269
+ t.Error (" ERROR: bytes contains" )
270
+ }
271
+ if containsBytesNot () == true {
272
+ t.Error (" ERROR: bytes contains not" )
273
+ }
274
+ }
275
+
276
+ func BenchmarkContainsBytes (b *testing .B ) {
277
+ for n := 0 ; n < b.N ; n++ {
278
+ containsBytes ()
279
+ }
280
+ }
281
+
282
+ func BenchmarkContainsBytesNot (b *testing .B ) {
283
+ for n := 0 ; n < b.N ; n++ {
284
+ containsBytesNot ()
285
+ }
286
+ }
287
+
288
+ // regexp.MustCompile + regexp.MatchString
289
+ func compileMatch (re *regexp .Regexp ) bool {
290
+ matched := re.MatchString (" Lorem Ipsum" )
291
+ return matched
292
+ }
293
+
294
+ func compileMatchNot (re *regexp .Regexp ) bool {
295
+ matched := re.MatchString (" Lorem Ipsum" )
296
+ return matched
297
+ }
298
+
299
+ func TestCompileMatch (t *testing .T ) {
300
+ re1 := regexp.MustCompile (" em Ip" )
301
+ re2 := regexp.MustCompile (" Dolor" )
302
+ if compileMatch (re1) == false {
303
+ t.Error (" ERROR: compile match" )
304
+ }
305
+ if compileMatchNot (re2) == true {
306
+ t.Error (" ERROR: compile match not" )
307
+ }
308
+ }
309
+
310
+ func BenchmarkCompileMatch (b *testing .B ) {
311
+ re := regexp.MustCompile (" em Ip" )
312
+ for n := 0 ; n < b.N ; n++ {
313
+ compileMatch (re)
314
+ }
315
+ }
316
+
317
+ func BenchmarkCompileMatchNot (b *testing .B ) {
318
+ re := regexp.MustCompile (" Dolor" )
319
+ for n := 0 ; n < b.N ; n++ {
320
+ compileMatchNot (re)
321
+ }
322
+ }
323
+
324
+ // regexp.MatchString
325
+ func match () bool {
326
+ matched , _ := regexp.MatchString (" em Ip" , " Lorem Ipsum" )
327
+ return matched
328
+ }
329
+
330
+ func matchNot () bool {
331
+ matched , _ := regexp.MatchString (" Dolor" , " Lorem Ipsum" )
332
+ return matched
333
+ }
334
+
335
+ func TestMatch (t *testing .T ) {
336
+ if match () == false {
337
+ t.Error (" ERROR: match" )
338
+ }
339
+ if matchNot () == true {
340
+ t.Error (" ERROR: match not" )
341
+ }
342
+ }
343
+
344
+ func BenchmarkMatch (b *testing .B ) {
345
+ for n := 0 ; n < b.N ; n++ {
346
+ match ()
347
+ }
348
+ }
349
+
350
+ func BenchmarkMatchNot (b *testing .B ) {
351
+ for n := 0 ; n < b.N ; n++ {
352
+ matchNot ()
353
+ }
354
+ }
355
+ ```
356
+
357
+ ```
358
+ $ go test -bench . -benchmem
359
+ goos: darwin
360
+ goarch: amd64
361
+ BenchmarkContains-8 100000000 14.7 ns/op 0 B/op 0 allocs/op
362
+ BenchmarkContainsNot-8 100000000 13.8 ns/op 0 B/op 0 allocs/op
363
+ BenchmarkContainsBytes-8 50000000 23.9 ns/op 0 B/op 0 allocs/op
364
+ BenchmarkContainsBytesNot-8 50000000 27.5 ns/op 0 B/op 0 allocs/op
365
+ BenchmarkCompileMatch-8 10000000 122 ns/op 0 B/op 0 allocs/op
366
+ BenchmarkCompileMatchNot-8 20000000 69.9 ns/op 0 B/op 0 allocs/op
367
+ BenchmarkMatch-8 200000 6561 ns/op 38912 B/op 27 allocs/op
368
+ BenchmarkMatchNot-8 300000 7574 ns/op 38912 B/op 27 allocs/op
369
+ PASS
370
+ ok _/Users/simonwaldherr/git/golang-benchmarks/contains 12.069s
371
+ ```
372
+
216
373
### foreach
217
374
218
375
``` go
@@ -304,6 +461,181 @@ PASS
304
461
ok _/Users/simonwaldherr/git/golang-benchmarks/foreach 7.600s
305
462
```
306
463
464
+ ### hash
465
+
466
+ ``` go
467
+ package hash
468
+
469
+ import (
470
+ " crypto/md5"
471
+ " crypto/sha1"
472
+ " crypto/sha256"
473
+ " crypto/sha512"
474
+ " github.com/jzelinskie/whirlpool"
475
+ " golang.org/x/crypto/sha3"
476
+ " hash"
477
+ " hash/adler32"
478
+ " hash/crc32"
479
+ " hash/fnv"
480
+ " math/rand"
481
+ " testing"
482
+ )
483
+
484
+ func benchmarkHashAlgo (b *testing .B , h hash .Hash ) {
485
+ data := make ([]byte , 2048 )
486
+ rand.Read (data)
487
+
488
+ b.ResetTimer ()
489
+ for n := 0 ; n < b.N ; n++ {
490
+ h.Write (data)
491
+ h.Sum (nil )
492
+ }
493
+ }
494
+
495
+ func BenchmarkAdler32 (b *testing .B ) {
496
+ benchmarkHashAlgo (b, adler32.New ())
497
+ }
498
+
499
+ func BenchmarkCRC32 (b *testing .B ) {
500
+ benchmarkHashAlgo (b, crc32.NewIEEE ())
501
+ }
502
+
503
+ func BenchmarkFnv128 (b *testing .B ) {
504
+ benchmarkHashAlgo (b, fnv.New128 ())
505
+ }
506
+
507
+ func BenchmarkMD5 (b *testing .B ) {
508
+ benchmarkHashAlgo (b, md5.New ())
509
+ }
510
+
511
+ func BenchmarkSHA1 (b *testing .B ) {
512
+ benchmarkHashAlgo (b, sha1.New ())
513
+ }
514
+
515
+ func BenchmarkSHA256 (b *testing .B ) {
516
+ benchmarkHashAlgo (b, sha256.New ())
517
+ }
518
+
519
+ func BenchmarkSHA512 (b *testing .B ) {
520
+ benchmarkHashAlgo (b, sha512.New ())
521
+ }
522
+
523
+ func BenchmarkSHA3256 (b *testing .B ) {
524
+ benchmarkHashAlgo (b, sha3.New256 ())
525
+ }
526
+
527
+ func BenchmarkSHA3512 (b *testing .B ) {
528
+ benchmarkHashAlgo (b, sha3.New512 ())
529
+ }
530
+
531
+ func BenchmarkWhirlpool (b *testing .B ) {
532
+ benchmarkHashAlgo (b, whirlpool.New ())
533
+ }
534
+ ```
535
+
536
+ ```
537
+ $ go test -bench . -benchmem
538
+ goos: darwin
539
+ goarch: amd64
540
+ BenchmarkAdler32-8 1000000 1052 ns/op 8 B/op 1 allocs/op
541
+ BenchmarkCRC32-8 10000000 193 ns/op 8 B/op 1 allocs/op
542
+ BenchmarkFnv128-8 200000 9713 ns/op 16 B/op 1 allocs/op
543
+ BenchmarkMD5-8 500000 3420 ns/op 16 B/op 1 allocs/op
544
+ BenchmarkSHA1-8 500000 2530 ns/op 32 B/op 1 allocs/op
545
+ BenchmarkSHA256-8 200000 5773 ns/op 32 B/op 1 allocs/op
546
+ BenchmarkSHA512-8 300000 4619 ns/op 64 B/op 1 allocs/op
547
+ BenchmarkSHA3256-8 200000 8197 ns/op 512 B/op 3 allocs/op
548
+ BenchmarkSHA3512-8 100000 14362 ns/op 576 B/op 3 allocs/op
549
+ BenchmarkWhirlpool-8 20000 70905 ns/op 64 B/op 1 allocs/op
550
+ PASS
551
+ ok _/Users/simonwaldherr/git/golang-benchmarks/hash 16.497s
552
+ ```
553
+
554
+ ### index
555
+
556
+ ``` go
557
+ package index
558
+
559
+ import (
560
+ " math/rand"
561
+ " strconv"
562
+ " testing"
563
+ )
564
+
565
+ var NumItems int = 1000000
566
+
567
+ var ms map [string ]string
568
+ var ks []string
569
+
570
+ var mi map [int ]string
571
+ var ki []int
572
+
573
+ func initMapStringIndex () {
574
+ ms = make (map [string ]string )
575
+ ks = make ([]string , 0 )
576
+
577
+ for i := 0 ; i < NumItems; i++ {
578
+ key := strconv.Itoa (rand.Intn (NumItems))
579
+ ms[key] = " value" + strconv.Itoa (i)
580
+ ks = append (ks, key)
581
+ }
582
+ }
583
+
584
+ func initMapIntIndex () {
585
+ mi = make (map [int ]string )
586
+ ki = make ([]int , 0 )
587
+
588
+ for i := 0 ; i < NumItems; i++ {
589
+ key := rand.Intn (NumItems)
590
+ mi[key] = " value" + strconv.Itoa (i)
591
+ ki = append (ki, key)
592
+ }
593
+ }
594
+
595
+ func init () {
596
+ initMapStringIndex ()
597
+ initMapIntIndex ()
598
+ }
599
+
600
+ func BenchmarkMapStringKeys (b *testing .B ) {
601
+ i := 0
602
+
603
+ for n := 0 ; n < b.N ; n++ {
604
+ if _ , ok := ms[ks[i]]; ok {
605
+ }
606
+
607
+ i++
608
+ if i >= NumItems {
609
+ i = 0
610
+ }
611
+ }
612
+ }
613
+
614
+ func BenchmarkMapIntKeys (b *testing .B ) {
615
+ i := 0
616
+
617
+ for n := 0 ; n < b.N ; n++ {
618
+ if _ , ok := mi[ki[i]]; ok {
619
+ }
620
+
621
+ i++
622
+ if i >= NumItems {
623
+ i = 0
624
+ }
625
+ }
626
+ }
627
+ ```
628
+
629
+ ```
630
+ $ go test -bench . -benchmem
631
+ goos: darwin
632
+ goarch: amd64
633
+ BenchmarkMapStringKeys-8 20000000 117 ns/op 0 B/op 0 allocs/op
634
+ BenchmarkMapIntKeys-8 20000000 84.2 ns/op 0 B/op 0 allocs/op
635
+ PASS
636
+ ok _/Users/simonwaldherr/git/golang-benchmarks/index 6.454s
637
+ ```
638
+
307
639
### parse
308
640
309
641
``` go
0 commit comments