{"id":22785894,"url":"https://github.com/simonwaldherr/golang-benchmarks","last_synced_at":"2025-04-05T02:08:43.836Z","repository":{"id":49349061,"uuid":"154216722","full_name":"SimonWaldherr/golang-benchmarks","owner":"SimonWaldherr","description":"Go(lang) benchmarks - (measure the speed of golang)","archived":false,"fork":false,"pushed_at":"2025-03-08T17:59:19.000Z","size":280,"stargazers_count":136,"open_issues_count":0,"forks_count":18,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-29T01:08:10.225Z","etag":null,"topics":["benchmark","benchmarking","benchmarks","education","examples","go","golang","golang-playground","hacktoberfest","hacktoberfest-accepted","howto","learning","testing"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SimonWaldherr.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"simonwaldherr","patreon":"simonwaldherr","open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":"SimonWaldherr","issuehunt":null,"otechie":null,"custom":["https://flattr.com/@SimonWaldherr"]}},"created_at":"2018-10-22T21:04:43.000Z","updated_at":"2025-03-08T17:59:23.000Z","dependencies_parsed_at":"2023-01-29T19:31:04.233Z","dependency_job_id":"9a2e947c-4164-4b44-8628-dc2e31ec633f","html_url":"https://github.com/SimonWaldherr/golang-benchmarks","commit_stats":{"total_commits":51,"total_committers":7,"mean_commits":7.285714285714286,"dds":"0.17647058823529416","last_synced_commit":"a43e6e8ae0d74b50899806efca5b78d7c1353330"},"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SimonWaldherr%2Fgolang-benchmarks","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SimonWaldherr%2Fgolang-benchmarks/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SimonWaldherr%2Fgolang-benchmarks/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SimonWaldherr%2Fgolang-benchmarks/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SimonWaldherr","download_url":"https://codeload.github.com/SimonWaldherr/golang-benchmarks/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247276164,"owners_count":20912288,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["benchmark","benchmarking","benchmarks","education","examples","go","golang","golang-playground","hacktoberfest","hacktoberfest-accepted","howto","learning","testing"],"created_at":"2024-12-11T23:08:22.330Z","updated_at":"2025-04-05T02:08:43.815Z","avatar_url":"https://github.com/SimonWaldherr.png","language":"Go","funding_links":["https://github.com/sponsors/simonwaldherr","https://patreon.com/simonwaldherr","https://liberapay.com/SimonWaldherr","https://flattr.com/@SimonWaldherr"],"categories":[],"sub_categories":[],"readme":"# Go Benchmarks\n\n[![test](https://github.com/SimonWaldherr/golang-benchmarks/actions/workflows/audit.yml/badge.svg?branch=master\u0026event=push)](https://github.com/SimonWaldherr/golang-benchmarks/actions/workflows/audit.yml) \n[![DOI](https://zenodo.org/badge/154216722.svg)](https://zenodo.org/badge/latestdoi/154216722) \n[![Go Report Card](https://goreportcard.com/badge/github.com/SimonWaldherr/golang-benchmarks)](https://goreportcard.com/report/github.com/SimonWaldherr/golang-benchmarks) \n[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT)  \n\nIn programming in general, and in Golang in particular, many roads lead to Rome.\nFrom time to time I ask myself which of these ways is the fastest. \nIn Golang there is a wonderful solution, with `go test -bench` you can measure the speed very easily and quickly.\nIn order for you to benefit from it too, I will publish such benchmarks in this repository in the future.\n\n## ToC\n\n* [base64](https://github.com/SimonWaldherr/golang-benchmarks#base64)\n* [between](https://github.com/SimonWaldherr/golang-benchmarks#between)\n* [caseinsensitivecompare](https://github.com/SimonWaldherr/golang-benchmarks#caseinsensitivecompare)\n* [concat](https://github.com/SimonWaldherr/golang-benchmarks#concat)\n* [contains](https://github.com/SimonWaldherr/golang-benchmarks#contains)\n* [embed](https://github.com/SimonWaldherr/golang-benchmarks#embed)\n* [floodfill](https://github.com/SimonWaldherr/golang-benchmarks#floodfill)\n* [foreach](https://github.com/SimonWaldherr/golang-benchmarks#foreach)\n* [hash](https://github.com/SimonWaldherr/golang-benchmarks#hash)\n* [index](https://github.com/SimonWaldherr/golang-benchmarks#index)\n* [json](https://github.com/SimonWaldherr/golang-benchmarks#json)\n* [math](https://github.com/SimonWaldherr/golang-benchmarks#math)\n* [parse](https://github.com/SimonWaldherr/golang-benchmarks#parse)\n* [random](https://github.com/SimonWaldherr/golang-benchmarks#random)\n* [regexp](https://github.com/SimonWaldherr/golang-benchmarks#regexp)\n* [template](https://github.com/SimonWaldherr/golang-benchmarks#template)\n* [trim](https://github.com/SimonWaldherr/golang-benchmarks#trim)\n\n## Golang?\n\nI published another repository where I show some Golang examples.\nIf you\\'re interested in new programming languages, you should definitely take a look at Golang:\n\n* [Golang examples](https://github.com/SimonWaldherr/golang-examples)\n* [tour.golang.org](https://tour.golang.org/)\n* [Go by example](https://gobyexample.com/)\n* [Golang Book](http://www.golang-book.com/)\n* [Go-Learn](https://github.com/skippednote/Go-Learn)\n\n## Is it any good?\n\n[Yes](https://news.ycombinator.com/item?id=3067434)\n\n## Benchmark Results\n\nGolang Version: [go version go1.24.1 darwin/arm64](https://tip.golang.org/doc/go1.24)  \nHardware Spec: [Apple MacBook Pro 16-Inch M2 Max 2023](https://support.apple.com/kb/SP890) [(?)](https://everymac.com/systems/apple/macbook_pro/specs/macbook-pro-m2-max-12-core-cpu-30-core-gpu-16-2023-specs.html) [(buy)](https://amzn.to/3K80lP4)  \n\n### base64\n\n```go\n// Package base64 benchmarks some base64 functions.\n// On all tested systems it's faster to decode a\n// base64 encoded string instead of a check via regex.\npackage base64\n\nimport (\n\t\"encoding/base64\"\n\t\"regexp\"\n\t\"testing\"\n)\n\nfunc base64decode(s string) bool {\n\t_, err := base64.StdEncoding.DecodeString(s)\n\treturn err == nil\n}\n\nfunc base64regex(s string) bool {\n\tmatched, _ := regexp.MatchString(`^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$`, s)\n\treturn matched\n}\n\nfunc BenchmarkBase64decode(b *testing.B) {\n\tisNotBase64 := `Invalid string`\n\tisBase64 := `VmFsaWQgc3RyaW5nCg==`\n\n\tfor n := 0; n \u003c b.N; n++ {\n\t\tbase64decode(isNotBase64)\n\t\tbase64decode(isBase64)\n\t}\n}\n\nfunc BenchmarkBase64regex(b *testing.B) {\n\tisNotBase64 := `Invalid string`\n\tisBase64 := `VmFsaWQgc3RyaW5nCg==`\n\n\tfor n := 0; n \u003c b.N; n++ {\n\t\tbase64regex(isNotBase64)\n\t\tbase64regex(isBase64)\n\t}\n}\n```\n\n```\n$ go test -bench . -benchmem\ngoos: darwin\ngoarch: arm64\ncpu: Apple M2 Max\nBenchmarkBase64decode-12    \t22993416\t        52.09 ns/op\t      32 B/op\t       2 allocs/op\nBenchmarkBase64regex-12     \t  134600\t      8962 ns/op\t   21896 B/op\t     198 allocs/op\nPASS\nok  \t_/Users/simonwaldherr/git/golang-benchmarks/base64\t3.670s\n```\n\n### between\n\n```go\n// Package between compares the performance of checking\n// if a number is between two other numbers via regex\n// and by parsing the number as integers.\npackage between\n\nimport (\n\t\"regexp\"\n\t\"simonwaldherr.de/go/golibs/as\"\n\t\"simonwaldherr.de/go/ranger\"\n\t\"testing\"\n)\n\nfunc BenchmarkNumberRegEx(b *testing.B) {\n\tre := ranger.Compile(89, 1001)\n\tre = \"^(\" + re + \")$\"\n\tb.ResetTimer()\n\n\tfor n := 0; n \u003c b.N; n++ {\n\t\tmatched, err := regexp.MatchString(re, \"404\")\n\t\tif !matched || err != nil {\n\t\t\tb.Log(\"Error in Benchmark\")\n\t\t}\n\n\t\tmatched, err = regexp.MatchString(re, \"2000\")\n\t\tif matched || err != nil {\n\t\t\tb.Log(\"Error in Benchmark\")\n\t\t}\n\t}\n}\n\nfunc BenchmarkFulltextRegEx(b *testing.B) {\n\tre := ranger.Compile(89, 1001)\n\tre = \" (\" + re + \") \"\n\tb.ResetTimer()\n\n\tfor n := 0; n \u003c b.N; n++ {\n\t\tmatched, err := regexp.MatchString(re, \"lorem ipsum 404 dolor sit\")\n\t\tif !matched || err != nil {\n\t\t\tb.Log(\"Error in Benchmark\")\n\t\t}\n\n\t\tmatched, err = regexp.MatchString(re, \"lorem ipsum 2000 dolor sit\")\n\t\tif matched || err != nil {\n\t\t\tb.Log(\"Error in Benchmark\")\n\t\t}\n\t}\n}\n\nfunc BenchmarkNumberParse(b *testing.B) {\n\tfor n := 0; n \u003c b.N; n++ {\n\t\ti1 := as.Int(\"404\")\n\t\ti2 := as.Int(\"2000\")\n\n\t\tif i1 \u003c 89 || i1 \u003e 1001 {\n\t\t\tb.Log(\"Error in Benchmark\")\n\t\t}\n\n\t\tif !(i2 \u003c 89 || i2 \u003e 1001) {\n\t\t\tb.Log(\"Error in Benchmark\")\n\t\t}\n\t}\n}\n\nfunc BenchmarkFulltextParse(b *testing.B) {\n\tre := regexp.MustCompile(\"[0-9]+\")\n\tb.ResetTimer()\n\n\tfor n := 0; n \u003c b.N; n++ {\n\t\ti1 := as.Int(re.FindString(\"lorem ipsum 404 dolor sit\"))\n\t\ti2 := as.Int(re.FindString(\"lorem ipsum 2000 dolor sit\"))\n\n\t\tif i1 \u003c 89 || i1 \u003e 1001 {\n\t\t\tb.Log(\"Error in Benchmark\")\n\t\t}\n\n\t\tif !(i2 \u003c 89 || i2 \u003e 1001) {\n\t\t\tb.Log(\"Error in Benchmark\")\n\t\t}\n\t}\n}\n```\n\n```\n$ go test -bench . -benchmem\ngoos: darwin\ngoarch: arm64\ncpu: Apple M2 Max\nBenchmarkNumberRegEx-12      \t  189409\t      6125 ns/op\t   16866 B/op\t     142 allocs/op\nBenchmarkFulltextRegEx-12    \t  244884\t      4903 ns/op\t   12084 B/op\t     104 allocs/op\nBenchmarkNumberParse-12      \t33922580\t        35.50 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkFulltextParse-12    \t 2527653\t       465.8 ns/op\t      32 B/op\t       2 allocs/op\nPASS\nok  \t_/Users/simonwaldherr/git/golang-benchmarks/between\t5.549s\n```\n\n### caseinsensitivecompare\n\n```go\npackage trim\n\nimport (\n\t\"strings\"\n\t\"testing\"\n)\n\nfunc BenchmarkEqualFold(b *testing.B) {\n\tfor n := 0; n \u003c b.N; n++ {\n\t\t_ = strings.EqualFold(\"abc\", \"ABC\")\n\t\t_ = strings.EqualFold(\"ABC\", \"ABC\")\n\t\t_ = strings.EqualFold(\"1aBcD\", \"1AbCd\")\n\t}\n}\n\nfunc BenchmarkToUpper(b *testing.B) {\n\tfor n := 0; n \u003c b.N; n++ {\n\t\t_ = strings.ToUpper(\"abc\") == strings.ToUpper(\"ABC\")\n\t\t_ = strings.ToUpper(\"ABC\") == strings.ToUpper(\"ABC\")\n\t\t_ = strings.ToUpper(\"1aBcD\") == strings.ToUpper(\"1AbCd\")\n\t}\n}\n\nfunc BenchmarkToLower(b *testing.B) {\n\tfor n := 0; n \u003c b.N; n++ {\n\t\t_ = strings.ToLower(\"abc\") == strings.ToLower(\"ABC\")\n\t\t_ = strings.ToLower(\"ABC\") == strings.ToLower(\"ABC\")\n\t\t_ = strings.ToLower(\"1aBcD\") == strings.ToLower(\"1AbCd\")\n\t}\n}\n```\n\n```\n$ go test -bench . -benchmem\ngoos: darwin\ngoarch: arm64\ncpu: Apple M2 Max\nBenchmarkEqualFold-12    \t83229736\t        14.77 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkToUpper-12      \t10451397\t       111.1 ns/op\t      24 B/op\t       3 allocs/op\nBenchmarkToLower-12      \t 8849136\t       135.1 ns/op\t      40 B/op\t       5 allocs/op\nPASS\nok  \t_/Users/simonwaldherr/git/golang-benchmarks/caseinsensitivecompare\t4.799s\n```\n\n### concat\n\n```go\n// Package concat benchmarks the performance of\n// various string concatenation methods.\n// Instead of just concatenating a string to another string\n// it is also possible (and much faster) to use\n// a bytes buffer.\npackage concat\n\nimport (\n\t\"bytes\"\n\t\"strings\"\n\t\"testing\"\n)\n\nfunc BenchmarkConcatString(b *testing.B) {\n\tvar str string\n\tfor n := 0; n \u003c b.N; n++ {\n\t\tstr += \"x\"\n\t}\n}\n\nfunc BenchmarkConcatBuffer(b *testing.B) {\n\tvar buffer bytes.Buffer\n\tfor n := 0; n \u003c b.N; n++ {\n\t\tbuffer.WriteString(\"x\")\n\n\t}\n}\n\nfunc BenchmarkConcatBuilder(b *testing.B) {\n\tvar builder strings.Builder\n\tfor n := 0; n \u003c b.N; n++ {\n\t\tbuilder.WriteString(\"x\")\n\t}\n}\n\nfunc BenchmarkConcat(b *testing.B) {\n\tb.Run(\"String\", func(b *testing.B) {\n\t\tvar str string\n\t\tfor n := 0; n \u003c b.N; n++ {\n\t\t\tstr += \"x\"\n\t\t}\n\t})\n\n\tb.Run(\"Buffer\", func(b *testing.B) {\n\t\tvar buffer bytes.Buffer\n\t\tfor n := 0; n \u003c b.N; n++ {\n\t\t\tbuffer.WriteString(\"x\")\n\t\t}\n\t})\n\n\tb.Run(\"Builder\", func(b *testing.B) {\n\t\tvar builder strings.Builder\n\t\tfor n := 0; n \u003c b.N; n++ {\n\t\t\tbuilder.WriteString(\"x\")\n\t\t}\n\t})\n}\n```\n\n```\n$ go test -bench . -benchmem\ngoos: darwin\ngoarch: arm64\ncpu: Apple M2 Max\nBenchmarkConcatString-12     \t 1000000\t     27267 ns/op\t  503994 B/op\t       1 allocs/op\nBenchmarkConcatBuffer-12     \t310347870\t         4.028 ns/op\t       3 B/op\t       0 allocs/op\nBenchmarkConcatBuilder-12    \t560170585\t         2.298 ns/op\t       5 B/op\t       0 allocs/op\nBenchmarkConcat/String-12    \t 1000000\t     28030 ns/op\t  503994 B/op\t       1 allocs/op\nBenchmarkConcat/Buffer-12    \t310996076\t         3.868 ns/op\t       3 B/op\t       0 allocs/op\nBenchmarkConcat/Builder-12   \t548751210\t         2.278 ns/op\t       5 B/op\t       0 allocs/op\nPASS\nok  \t_/Users/simonwaldherr/git/golang-benchmarks/concat\t61.738s\n```\n\n### contains\n\n```go\n// Package contains tests various ways of checking\n// if a string is contained in another string.\npackage contains\n\nimport (\n\t\"bytes\"\n\t\"regexp\"\n\t\"strings\"\n\t\"testing\"\n)\n\n// strings.Contains\nfunc contains() bool {\n\treturn strings.Contains(\"Lorem Ipsum\", \"em Ip\")\n}\n\nfunc containsNot() bool {\n\treturn strings.Contains(\"Lorem Ipsum\", \"Dolor\")\n}\n\nfunc TestContains(t *testing.T) {\n\tif contains() == false {\n\t\tt.Error(\"ERROR: contains\")\n\t}\n\tif containsNot() == true {\n\t\tt.Error(\"ERROR: contains not\")\n\t}\n}\n\nfunc BenchmarkContains(b *testing.B) {\n\tfor n := 0; n \u003c b.N; n++ {\n\t\tcontains()\n\t}\n}\n\nfunc BenchmarkContainsNot(b *testing.B) {\n\tfor n := 0; n \u003c b.N; n++ {\n\t\tcontainsNot()\n\t}\n}\n\n// bytes.Contains\nfunc containsBytes() bool {\n\treturn bytes.Contains([]byte(\"Lorem Ipsum\"), []byte(\"em Ip\"))\n}\n\nfunc containsBytesNot() bool {\n\treturn bytes.Contains([]byte(\"Lorem Ipsum\"), []byte(\"Dolor\"))\n}\n\nfunc TestContainsBytes(t *testing.T) {\n\tif containsBytes() == false {\n\t\tt.Error(\"ERROR: bytes contains\")\n\t}\n\tif containsBytesNot() == true {\n\t\tt.Error(\"ERROR: bytes contains not\")\n\t}\n}\n\nfunc BenchmarkContainsBytes(b *testing.B) {\n\tfor n := 0; n \u003c b.N; n++ {\n\t\tcontainsBytes()\n\t}\n}\n\nfunc BenchmarkContainsBytesNot(b *testing.B) {\n\tfor n := 0; n \u003c b.N; n++ {\n\t\tcontainsBytesNot()\n\t}\n}\n\n// regexp.MustCompile + regexp.MatchString\nfunc compileMatch(re *regexp.Regexp) bool {\n\tmatched := re.MatchString(\"Lorem Ipsum\")\n\treturn matched\n}\n\nfunc compileMatchNot(re *regexp.Regexp) bool {\n\tmatched := re.MatchString(\"Lorem Ipsum\")\n\treturn matched\n}\n\nfunc TestCompileMatch(t *testing.T) {\n\tre1 := regexp.MustCompile(\"em Ip\")\n\tre2 := regexp.MustCompile(\"Dolor\")\n\tif compileMatch(re1) == false {\n\t\tt.Error(\"ERROR: compile match\")\n\t}\n\tif compileMatchNot(re2) == true {\n\t\tt.Error(\"ERROR: compile match not\")\n\t}\n}\n\nfunc BenchmarkCompileMatch(b *testing.B) {\n\tre := regexp.MustCompile(\"em Ip\")\n\tfor n := 0; n \u003c b.N; n++ {\n\t\tcompileMatch(re)\n\t}\n}\n\nfunc BenchmarkCompileMatchNot(b *testing.B) {\n\tre := regexp.MustCompile(\"Dolor\")\n\tfor n := 0; n \u003c b.N; n++ {\n\t\tcompileMatchNot(re)\n\t}\n}\n\n// regexp.MatchString\nfunc match() bool {\n\tmatched, _ := regexp.MatchString(\"em Ip\", \"Lorem Ipsum\")\n\treturn matched\n}\n\nfunc matchNot() bool {\n\tmatched, _ := regexp.MatchString(\"Dolor\", \"Lorem Ipsum\")\n\treturn matched\n}\n\nfunc TestMatch(t *testing.T) {\n\tif match() == false {\n\t\tt.Error(\"ERROR: match\")\n\t}\n\tif matchNot() == true {\n\t\tt.Error(\"ERROR: match not\")\n\t}\n}\n\nfunc BenchmarkMatch(b *testing.B) {\n\tfor n := 0; n \u003c b.N; n++ {\n\t\tmatch()\n\t}\n}\n\nfunc BenchmarkMatchNot(b *testing.B) {\n\tfor n := 0; n \u003c b.N; n++ {\n\t\tmatchNot()\n\t}\n}\n\n// BenchmarkContainsMethods benchmarks different methods to check substring presence.\nfunc BenchmarkContainsMethods(b *testing.B) {\n\tb.Run(\"Strings.Contains\", func(b *testing.B) {\n\t\tstr := \"Lorem Ipsum\"\n\t\tsubstr := \"em Ip\"\n\t\tfor n := 0; n \u003c b.N; n++ {\n\t\t\t_ = strings.Contains(str, substr)\n\t\t\t_ = strings.Contains(str, \"Dolor\")\n\t\t}\n\t})\n\n\tb.Run(\"Bytes.Contains\", func(b *testing.B) {\n\t\tstr := []byte(\"Lorem Ipsum\")\n\t\tsubstr := []byte(\"em Ip\")\n\t\tfor n := 0; n \u003c b.N; n++ {\n\t\t\t_ = bytes.Contains(str, substr)\n\t\t\t_ = bytes.Contains(str, []byte(\"Dolor\"))\n\t\t}\n\t})\n\n\tb.Run(\"RegexMatchString\", func(b *testing.B) {\n\t\tre := regexp.MustCompile(`em Ip`)\n\t\tfor n := 0; n \u003c b.N; n++ {\n\t\t\t_ = re.MatchString(\"Lorem Ipsum\")\n\t\t\t_ = re.MatchString(\"Dolor\")\n\t\t}\n\t})\n\n\tb.Run(\"RegexMatch\", func(b *testing.B) {\n\t\tfor n := 0; n \u003c b.N; n++ {\n\t\t\t_, _ = regexp.MatchString(`em Ip`, \"Lorem Ipsum\")\n\t\t\t_, _ = regexp.MatchString(`em Ip`, \"Dolor\")\n\t\t}\n\t})\n}\n```\n\n```\n$ go test -bench . -benchmem\ngoos: darwin\ngoarch: arm64\ncpu: Apple M2 Max\nBenchmarkContains-12            \t239120871\t         4.882 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkContainsNot-12         \t192422649\t         6.099 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkContainsBytes-12       \t219692301\t         5.481 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkContainsBytesNot-12    \t179861270\t         6.646 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkCompileMatch-12        \t25366514\t        48.32 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkCompileMatchNot-12     \t48535671\t        24.42 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkMatch-12               \t 1697743\t       677.4 ns/op\t    1398 B/op\t      17 allocs/op\nBenchmarkMatchNot-12            \t 1945729\t       644.8 ns/op\t    1398 B/op\t      17 allocs/op\nBenchmarkContainsMethods/Strings.Contains-12         \t100000000\t        10.69 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkContainsMethods/Bytes.Contains-12           \t100000000\t        11.22 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkContainsMethods/RegexMatchString-12         \t17722814\t        67.89 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkContainsMethods/RegexMatch-12               \t  900624\t      1294 ns/op\t    2797 B/op\t      34 allocs/op\nPASS\nok  \t_/Users/simonwaldherr/git/golang-benchmarks/contains\t18.203s\n```\n\n### embed\n\n```go\npackage embed\n\nimport (\n\t_ \"embed\"\n\t\"io/ioutil\"\n\t\"os\"\n\t\"testing\"\n)\n\n//go:embed example.txt\nvar embeddedFile []byte\n\nfunc BenchmarkEmbed(b *testing.B) {\n\tfor i := 0; i \u003c b.N; i++ {\n\t\t// Access the embedded file\n\t\t_ = embeddedFile\n\t}\n}\n\nfunc BenchmarkReadFile(b *testing.B) {\n\tfor i := 0; i \u003c b.N; i++ {\n\t\t// Read the file from disk\n\t\tdata, err := os.ReadFile(\"example.txt\")\n\t\tif err != nil {\n\t\t\tb.Fatalf(\"failed to read file: %v\", err)\n\t\t}\n\t\t_ = data\n\t}\n}\n\nfunc BenchmarkIoutilReadFile(b *testing.B) {\n\tfor i := 0; i \u003c b.N; i++ {\n\t\t// Read the file using ioutil\n\t\tdata, err := ioutil.ReadFile(\"example.txt\")\n\t\tif err != nil {\n\t\t\tb.Fatalf(\"failed to read file: %v\", err)\n\t\t}\n\t\t_ = data\n\t}\n}\n```\n\n```\n$ go test -bench . -benchmem\ngoos: darwin\ngoarch: arm64\ncpu: Apple M2 Max\nBenchmarkEmbed-12             \t1000000000\t         0.3028 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkReadFile-12          \t  155815\t      7606 ns/op\t     840 B/op\t       5 allocs/op\nBenchmarkIoutilReadFile-12    \t  162920\t      7656 ns/op\t     840 B/op\t       5 allocs/op\nPASS\nok  \t_/Users/simonwaldherr/git/golang-benchmarks/embed\t3.134s\n```\n\n### floodfill\n\n```go\n// Package floodfill benchmarks various flood fill implementations.\npackage main\n\nimport (\n\t\"testing\"\n)\n\n// Rekursive Implementierung\nfunc floodFillRecursive(image [][]int, sr int, sc int, newColor int) [][]int {\n\toldColor := image[sr][sc]\n\tif oldColor == newColor {\n\t\treturn image\n\t}\n\tfloodFillRecurse(image, sr, sc, oldColor, newColor)\n\treturn image\n}\n\nfunc floodFillRecurse(image [][]int, sr int, sc int, oldColor int, newColor int) {\n\tif sr \u003c 0 || sr \u003e= len(image) || sc \u003c 0 || sc \u003e= len(image[0]) || image[sr][sc] != oldColor {\n\t\treturn\n\t}\n\timage[sr][sc] = newColor\n\n\tfloodFillRecurse(image, sr+1, sc, oldColor, newColor)\n\tfloodFillRecurse(image, sr-1, sc, oldColor, newColor)\n\tfloodFillRecurse(image, sr, sc+1, oldColor, newColor)\n\tfloodFillRecurse(image, sr, sc-1, oldColor, newColor)\n}\n\n// Iterative Implementierung mit Stack (DFS)\nfunc floodFillDFS(image [][]int, sr int, sc int, newColor int) [][]int {\n\toldColor := image[sr][sc]\n\tif oldColor == newColor {\n\t\treturn image\n\t}\n\n\tstack := [][]int{{sr, sc}}\n\tfor len(stack) \u003e 0 {\n\t\tcurrent := stack[len(stack)-1]\n\t\tstack = stack[:len(stack)-1]\n\n\t\tr, c := current[0], current[1]\n\t\tif r \u003c 0 || r \u003e= len(image) || c \u003c 0 || c \u003e= len(image[0]) || image[r][c] != oldColor {\n\t\t\tcontinue\n\t\t}\n\n\t\timage[r][c] = newColor\n\n\t\tstack = append(stack, []int{r + 1, c})\n\t\tstack = append(stack, []int{r - 1, c})\n\t\tstack = append(stack, []int{r, c + 1})\n\t\tstack = append(stack, []int{r, c - 1})\n\t}\n\treturn image\n}\n\n// Iterative Implementierung mit Queue (BFS)\nfunc floodFillBFS(image [][]int, sr int, sc int, newColor int) [][]int {\n\toldColor := image[sr][sc]\n\tif oldColor == newColor {\n\t\treturn image\n\t}\n\n\tqueue := [][]int{{sr, sc}}\n\tfor len(queue) \u003e 0 {\n\t\tcurrent := queue[0]\n\t\tqueue = queue[1:]\n\n\t\tr, c := current[0], current[1]\n\t\tif r \u003c 0 || r \u003e= len(image) || c \u003c 0 || c \u003e= len(image[0]) || image[r][c] != oldColor {\n\t\t\tcontinue\n\t\t}\n\n\t\timage[r][c] = newColor\n\n\t\tqueue = append(queue, []int{r + 1, c})\n\t\tqueue = append(queue, []int{r - 1, c})\n\t\tqueue = append(queue, []int{r, c + 1})\n\t\tqueue = append(queue, []int{r, c - 1})\n\t}\n\treturn image\n}\n\n// Iterative Implementierung mit Stack (4-Wege-Verbindung)\nfunc floodFillStack4Way(image [][]int, sr int, sc int, newColor int) [][]int {\n\toldColor := image[sr][sc]\n\tif oldColor == newColor {\n\t\treturn image\n\t}\n\n\tstack := [][]int{{sr, sc}}\n\tdirections := [][]int{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}\n\tfor len(stack) \u003e 0 {\n\t\tcurrent := stack[len(stack)-1]\n\t\tstack = stack[:len(stack)-1]\n\n\t\tr, c := current[0], current[1]\n\t\tif r \u003c 0 || r \u003e= len(image) || c \u003c 0 || c \u003e= len(image[0]) || image[r][c] != oldColor {\n\t\t\tcontinue\n\t\t}\n\n\t\timage[r][c] = newColor\n\n\t\tfor _, dir := range directions {\n\t\t\tstack = append(stack, []int{r + dir[0], c + dir[1]})\n\t\t}\n\t}\n\treturn image\n}\n\n// Komplexes Beispielbild\nvar complexImage = [][]int{\n\t{0, 0, 0, 0, 0, 0},\n\t{0, 1, 1, 0, 1, 0},\n\t{0, 1, 0, 0, 1, 0},\n\t{0, 1, 1, 1, 1, 0},\n\t{0, 0, 0, 0, 0, 0},\n\t{0, 1, 1, 0, 1, 1},\n}\n\n// Benchmark für die rekursive Implementierung\nfunc BenchmarkFloodFillRecursive(b *testing.B) {\n\tfor i := 0; i \u003c b.N; i++ {\n\t\timage := make([][]int, len(complexImage))\n\t\tfor j := range complexImage {\n\t\t\timage[j] = make([]int, len(complexImage[j]))\n\t\t\tcopy(image[j], complexImage[j])\n\t\t}\n\t\tfloodFillRecursive(image, 1, 1, 2)\n\t}\n}\n\n// Benchmark für die iterative Implementierung mit Stack (DFS)\nfunc BenchmarkFloodFillDFS(b *testing.B) {\n\tfor i := 0; i \u003c b.N; i++ {\n\t\timage := make([][]int, len(complexImage))\n\t\tfor j := range complexImage {\n\t\t\timage[j] = make([]int, len(complexImage[j]))\n\t\t\tcopy(image[j], complexImage[j])\n\t\t}\n\t\tfloodFillDFS(image, 1, 1, 2)\n\t}\n}\n\n// Benchmark für die iterative Implementierung mit Queue (BFS)\nfunc BenchmarkFloodFillBFS(b *testing.B) {\n\tfor i := 0; i \u003c b.N; i++ {\n\t\timage := make([][]int, len(complexImage))\n\t\tfor j := range complexImage {\n\t\t\timage[j] = make([]int, len(complexImage[j]))\n\t\t\tcopy(image[j], complexImage[j])\n\t\t}\n\t\tfloodFillBFS(image, 1, 1, 2)\n\t}\n}\n\n// Benchmark für die iterative Implementierung mit Stack (4-Wege-Verbindung)\nfunc BenchmarkFloodFillStack4Way(b *testing.B) {\n\tfor i := 0; i \u003c b.N; i++ {\n\t\timage := make([][]int, len(complexImage))\n\t\tfor j := range complexImage {\n\t\t\timage[j] = make([]int, len(complexImage[j]))\n\t\t\tcopy(image[j], complexImage[j])\n\t\t}\n\t\tfloodFillStack4Way(image, 1, 1, 2)\n\t}\n}\n```\n\n```\n$ go test -bench . -benchmem\ngoos: darwin\ngoarch: arm64\ncpu: Apple M2 Max\nBenchmarkFloodFillRecursive-12    \t 5880130\t       181.5 ns/op\t     432 B/op\t       7 allocs/op\nBenchmarkFloodFillDFS-12          \t 1486161\t       802.0 ns/op\t    1744 B/op\t      48 allocs/op\nBenchmarkFloodFillBFS-12          \t 1000000\t      1007 ns/op\t    2728 B/op\t      54 allocs/op\nBenchmarkFloodFillStack4Way-12    \t 1419745\t       839.9 ns/op\t    1744 B/op\t      48 allocs/op\nPASS\nok  \t_/Users/simonwaldherr/git/golang-benchmarks/floodfill\t6.532s\n```\n\n### foreach\n\n```go\n// Package foreach benchmarks ranging over slices and maps.\npackage foreach\n\nimport (\n\t\"testing\"\n)\n\nvar amap map[int]string\nvar aslice []string\n\nfunc init() {\n\tamap = map[int]string{\n\t\t0: \"lorem\",\n\t\t1: \"ipsum\",\n\t\t2: \"dolor\",\n\t\t3: \"sit\",\n\t\t4: \"amet\",\n\t}\n\n\taslice = []string{\n\t\t\"lorem\",\n\t\t\"ipsum\",\n\t\t\"dolor\",\n\t\t\"sit\",\n\t\t\"amet\",\n\t}\n}\n\nfunc forMap() {\n\tfor i := 0; i \u003c len(amap); i++ {\n\t\t_ = amap[i]\n\t}\n}\n\nfunc rangeMap() {\n\tfor _, v := range amap {\n\t\t_ = v\n\t}\n}\n\nfunc rangeSlice() {\n\tfor _, v := range aslice {\n\t\t_ = v\n\t}\n}\n\nfunc rangeSliceKey() {\n\tfor k := range aslice {\n\t\t_ = aslice[k]\n\t}\n}\n\nfunc BenchmarkForMap(b *testing.B) {\n\tfor n := 0; n \u003c b.N; n++ {\n\t\tforMap()\n\t}\n}\n\nfunc BenchmarkRangeMap(b *testing.B) {\n\tfor n := 0; n \u003c b.N; n++ {\n\t\trangeMap()\n\t}\n}\n\nfunc BenchmarkRangeSlice(b *testing.B) {\n\tfor n := 0; n \u003c b.N; n++ {\n\t\trangeSlice()\n\t}\n}\n\nfunc BenchmarkRangeSliceKey(b *testing.B) {\n\tfor n := 0; n \u003c b.N; n++ {\n\t\trangeSliceKey()\n\t}\n}\n```\n\n```\n$ go test -bench . -benchmem\ngoos: darwin\ngoarch: arm64\ncpu: Apple M2 Max\nBenchmarkForMap-12           \t64127058\t        18.24 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkRangeMap-12         \t29853902\t        40.11 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkRangeSlice-12       \t453025074\t         2.579 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkRangeSliceKey-12    \t460315377\t         2.578 ns/op\t       0 B/op\t       0 allocs/op\nPASS\nok  \t_/Users/simonwaldherr/git/golang-benchmarks/foreach\t6.197s\n```\n\n### hash\n\n```go\n// Package hash benchmarks various hashing algorithms.\n// Especially with hashing algorithms, faster is not always better.\n// One should always decide on the basis of the respective requirements.\npackage hash\n\nimport (\n\t\"crypto/md5\"\n\t\"crypto/sha1\"\n\t\"crypto/sha256\"\n\t\"crypto/sha512\"\n\t\"hash\"\n\t\"hash/adler32\"\n\t\"hash/crc32\"\n\t\"hash/crc64\"\n\t\"hash/fnv\"\n\t\"math/rand\"\n\t\"testing\"\n\n\t\"github.com/jzelinskie/whirlpool\"\n\t\"github.com/reusee/mmh3\"\n\t\"github.com/zeebo/blake3\"\n\t\"golang.org/x/crypto/bcrypt\"\n\t\"golang.org/x/crypto/blake2b\"\n\t\"golang.org/x/crypto/md4\"\n\t\"golang.org/x/crypto/ripemd160\"\n\t\"golang.org/x/crypto/sha3\"\n)\n\nfunc benchmarkHashAlgo(b *testing.B, h hash.Hash) {\n\tdata := make([]byte, 2048)\n\trand.Read(data)\n\n\tb.ResetTimer()\n\tfor n := 0; n \u003c b.N; n++ {\n\t\th.Reset()\n\t\th.Write(data)\n\t\t_ = h.Sum(nil)\n\t}\n}\n\nfunc benchmarkBCryptHashAlgo(b *testing.B, cost int) {\n\tdata := make([]byte, 2048)\n\trand.Read(data)\n\n\tb.ResetTimer()\n\tfor n := 0; n \u003c b.N; n++ {\n\t\tbcrypt.GenerateFromPassword(data, cost)\n\t}\n}\n\nfunc BenchmarkAdler32(b *testing.B) {\n\tbenchmarkHashAlgo(b, adler32.New())\n}\n\nfunc BenchmarkBCryptCost4(b *testing.B) {\n\tbenchmarkBCryptHashAlgo(b, 4)\n}\n\nfunc BenchmarkBCryptCost10(b *testing.B) {\n\tbenchmarkBCryptHashAlgo(b, 10)\n}\n\nfunc BenchmarkBCryptCost16(b *testing.B) {\n\tbenchmarkBCryptHashAlgo(b, 16)\n}\n\n/*\nfunc BenchmarkBCryptCost22(b *testing.B) {\n\tbenchmarkBCryptHashAlgo(b, 22)\n}\n\nfunc BenchmarkBCryptCost28(b *testing.B) {\n\tbenchmarkBCryptHashAlgo(b, 28)\n}\n\nfunc BenchmarkBCryptCost31(b *testing.B) {\n\tbenchmarkBCryptHashAlgo(b, 31)\n}\n*/\n\nfunc BenchmarkBlake2b256(b *testing.B) {\n\th, err := blake2b.New256(nil)\n\tif err != nil {\n\t\tb.Fatal(err)\n\t}\n\tbenchmarkHashAlgo(b, h)\n}\n\nfunc BenchmarkBlake2b512(b *testing.B) {\n\th, err := blake2b.New512(nil)\n\tif err != nil {\n\t\tb.Fatal(err)\n\t}\n\tbenchmarkHashAlgo(b, h)\n}\n\nfunc BenchmarkBlake3256(b *testing.B) {\n\tbenchmarkHashAlgo(b, blake3.New())\n}\n\nfunc BenchmarkMMH3(b *testing.B) {\n\tbenchmarkHashAlgo(b, mmh3.New128())\n}\n\nfunc BenchmarkCRC32(b *testing.B) {\n\tbenchmarkHashAlgo(b, crc32.NewIEEE())\n}\n\nfunc BenchmarkCRC64ISO(b *testing.B) {\n\tbenchmarkHashAlgo(b, crc64.New(crc64.MakeTable(crc64.ISO)))\n}\n\nfunc BenchmarkCRC64ECMA(b *testing.B) {\n\tbenchmarkHashAlgo(b, crc64.New(crc64.MakeTable(crc64.ECMA)))\n}\n\nfunc BenchmarkFnv32(b *testing.B) {\n\tbenchmarkHashAlgo(b, fnv.New32())\n}\n\nfunc BenchmarkFnv32a(b *testing.B) {\n\tbenchmarkHashAlgo(b, fnv.New32a())\n}\n\nfunc BenchmarkFnv64(b *testing.B) {\n\tbenchmarkHashAlgo(b, fnv.New64())\n}\n\nfunc BenchmarkFnv64a(b *testing.B) {\n\tbenchmarkHashAlgo(b, fnv.New64a())\n}\n\nfunc BenchmarkFnv128(b *testing.B) {\n\tbenchmarkHashAlgo(b, fnv.New128())\n}\n\nfunc BenchmarkFnv128a(b *testing.B) {\n\tbenchmarkHashAlgo(b, fnv.New128a())\n}\n\nfunc BenchmarkMD4(b *testing.B) {\n\tbenchmarkHashAlgo(b, md4.New())\n}\n\nfunc BenchmarkMD5(b *testing.B) {\n\tbenchmarkHashAlgo(b, md5.New())\n}\n\nfunc BenchmarkSHA1(b *testing.B) {\n\tbenchmarkHashAlgo(b, sha1.New())\n}\n\nfunc BenchmarkSHA224(b *testing.B) {\n\tbenchmarkHashAlgo(b, sha256.New224())\n}\n\nfunc BenchmarkSHA256(b *testing.B) {\n\tbenchmarkHashAlgo(b, sha256.New())\n}\n\nfunc BenchmarkSHA384(b *testing.B) {\n\tbenchmarkHashAlgo(b, sha512.New384())\n}\n\nfunc BenchmarkSHA512(b *testing.B) {\n\tbenchmarkHashAlgo(b, sha512.New())\n}\n\nfunc BenchmarkSHA3256(b *testing.B) {\n\tbenchmarkHashAlgo(b, sha3.New256())\n}\n\nfunc BenchmarkSHA3512(b *testing.B) {\n\tbenchmarkHashAlgo(b, sha3.New512())\n}\n\nfunc BenchmarkRIPEMD160(b *testing.B) {\n\tbenchmarkHashAlgo(b, ripemd160.New())\n}\n\nfunc BenchmarkWhirlpool(b *testing.B) {\n\tbenchmarkHashAlgo(b, whirlpool.New())\n}\n\nfunc BenchmarkSHA256Parallel(b *testing.B) {\n\tb.RunParallel(func(pb *testing.PB) {\n\t\tdata := make([]byte, 2048)\n\t\trand.Read(data)\n\t\tfor pb.Next() {\n\t\t\th := sha256.New()\n\t\t\th.Write(data)\n\t\t\th.Sum(nil)\n\t\t}\n\t})\n}\n```\n\n```\n$ go test -bench . -benchmem\ngoos: darwin\ngoarch: arm64\ncpu: Apple M2 Max\nBenchmarkAdler32-12           \t 1759833\t       661.4 ns/op\t       8 B/op\t       1 allocs/op\nBenchmarkBCryptCost4-12       \t624677763\t         1.921 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkBCryptCost10-12      \t624473506\t         1.926 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkBCryptCost16-12      \t624608128\t         1.929 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkBlake2b256-12        \t  473914\t      2532 ns/op\t      32 B/op\t       1 allocs/op\nBenchmarkBlake2b512-12        \t  472370\t      2555 ns/op\t      64 B/op\t       1 allocs/op\nBenchmarkBlake3256-12         \t  411027\t      2879 ns/op\t      64 B/op\t       2 allocs/op\nBenchmarkMMH3-12              \t 3816295\t       314.4 ns/op\t      16 B/op\t       1 allocs/op\nBenchmarkCRC32-12             \t 5241662\t       228.8 ns/op\t       8 B/op\t       1 allocs/op\nBenchmarkCRC64ISO-12          \t 1000000\t      1100 ns/op\t       8 B/op\t       1 allocs/op\nBenchmarkCRC64ECMA-12         \t 1000000\t      1098 ns/op\t       8 B/op\t       1 allocs/op\nBenchmarkFnv32-12             \t  492052\t      2386 ns/op\t       8 B/op\t       1 allocs/op\nBenchmarkFnv32a-12            \t  513301\t      2405 ns/op\t       8 B/op\t       1 allocs/op\nBenchmarkFnv64-12             \t  504188\t      2369 ns/op\t       8 B/op\t       1 allocs/op\nBenchmarkFnv64a-12            \t  507104\t      2351 ns/op\t       8 B/op\t       1 allocs/op\nBenchmarkFnv128-12            \t  310280\t      3926 ns/op\t      24 B/op\t       2 allocs/op\nBenchmarkFnv128a-12           \t  474702\t      2535 ns/op\t      24 B/op\t       2 allocs/op\nBenchmarkMD4-12               \t  414134\t      2924 ns/op\t      24 B/op\t       2 allocs/op\nBenchmarkMD5-12               \t  408540\t      3027 ns/op\t      16 B/op\t       1 allocs/op\nBenchmarkSHA1-12              \t 1403704\t       838.5 ns/op\t      24 B/op\t       1 allocs/op\nBenchmarkSHA224-12            \t 1434771\t       851.7 ns/op\t      32 B/op\t       1 allocs/op\nBenchmarkSHA256-12            \t 1443553\t       829.1 ns/op\t      32 B/op\t       1 allocs/op\nBenchmarkSHA384-12            \t  840198\t      1454 ns/op\t      48 B/op\t       1 allocs/op\nBenchmarkSHA512-12            \t  840021\t      1436 ns/op\t      64 B/op\t       1 allocs/op\nBenchmarkSHA3256-12           \t  229534\t      5202 ns/op\t     480 B/op\t       2 allocs/op\nBenchmarkSHA3512-12           \t  131982\t      9066 ns/op\t     512 B/op\t       2 allocs/op\nBenchmarkRIPEMD160-12         \t  193982\t      6069 ns/op\t      24 B/op\t       1 allocs/op\nBenchmarkWhirlpool-12         \t   44787\t     26869 ns/op\t      64 B/op\t       1 allocs/op\nBenchmarkSHA256Parallel-12    \t12842282\t        94.25 ns/op\t      32 B/op\t       1 allocs/op\nPASS\nok  \t_/Users/simonwaldherr/git/golang-benchmarks/hash\t40.142s\n```\n\n### index\n\n```go\n// Package index benchmarks access on maps with various data types as keys.\npackage index\n\nimport (\n\t\"math/rand\"\n\t\"strconv\"\n\t\"testing\"\n)\n\nvar NumItems int = 1000000\n\nvar ms map[string]string\nvar ks []string\n\nvar mi map[int]string\nvar ki []int\n\nfunc initMapStringIndex() {\n\tms = make(map[string]string)\n\tks = make([]string, 0)\n\n\tfor i := 0; i \u003c NumItems; i++ {\n\t\tkey := strconv.Itoa(rand.Intn(NumItems))\n\t\tms[key] = \"value\" + strconv.Itoa(i)\n\t\tks = append(ks, key)\n\t}\n}\n\nfunc initMapIntIndex() {\n\tmi = make(map[int]string)\n\tki = make([]int, 0)\n\n\tfor i := 0; i \u003c NumItems; i++ {\n\t\tkey := rand.Intn(NumItems)\n\t\tmi[key] = \"value\" + strconv.Itoa(i)\n\t\tki = append(ki, key)\n\t}\n}\n\nfunc init() {\n\tinitMapStringIndex()\n\tinitMapIntIndex()\n}\n\nfunc BenchmarkMapStringKeys(b *testing.B) {\n\ti := 0\n\n\tfor n := 0; n \u003c b.N; n++ {\n\t\tif _, ok := ms[ks[i]]; ok {\n\t\t}\n\n\t\ti++\n\t\tif i \u003e= NumItems {\n\t\t\ti = 0\n\t\t}\n\t}\n}\n\nfunc BenchmarkMapIntKeys(b *testing.B) {\n\ti := 0\n\n\tfor n := 0; n \u003c b.N; n++ {\n\t\tif _, ok := mi[ki[i]]; ok {\n\t\t}\n\n\t\ti++\n\t\tif i \u003e= NumItems {\n\t\t\ti = 0\n\t\t}\n\t}\n}\n\nfunc BenchmarkMapStringIndex(b *testing.B) {\n\ti := 0\n\n\tfor n := 0; n \u003c b.N; n++ {\n\t\t_ = ms[ks[i]]\n\n\t\ti++\n\t\tif i \u003e= NumItems {\n\t\t\ti = 0\n\t\t}\n\t}\n}\n\nfunc BenchmarkMapIntIndex(b *testing.B) {\n\ti := 0\n\n\tfor n := 0; n \u003c b.N; n++ {\n\t\t_ = mi[ki[i]]\n\n\t\ti++\n\t\tif i \u003e= NumItems {\n\t\t\ti = 0\n\t\t}\n\t}\n}\n```\n\n```\n$ go test -bench . -benchmem\ngoos: darwin\ngoarch: arm64\ncpu: Apple M2 Max\nBenchmarkMapStringKeys-12     \t25611040\t        49.14 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkMapIntKeys-12        \t73193930\t        15.91 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkMapStringIndex-12    \t26859706\t        41.25 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkMapIntIndex-12       \t77115676\t        17.09 ns/op\t       0 B/op\t       0 allocs/op\nPASS\nok  \t_/Users/simonwaldherr/git/golang-benchmarks/index\t8.838s\n```\n\n### json\n\n```go\npackage json\n\nimport (\n\t\"encoding/json\"\n\t\"math\"\n\t\"math/big\"\n\t\"testing\"\n\t\"time\"\n)\n\ntype Data struct {\n\tString   string\n\tTime     time.Time\n\tInt      int\n\tInt8     int8\n\tInt16    int16\n\tInt32    int32\n\tInt64    int64\n\tBoolean  bool\n\tFloat32  float32\n\tFloat64  float64\n\tBigInt   big.Int\n\tBigFloat big.Float\n}\n\nfunc BenchmarkJsonMarshal(b *testing.B) {\n\tfor n := 0; n \u003c b.N; n++ {\n\t\tvar d = Data{\n\t\t\tString:   \"\",\n\t\t\tTime:     time.Now(),\n\t\t\tInt:      math.MaxInt32,\n\t\t\tInt8:     math.MaxInt8,\n\t\t\tInt16:    math.MaxInt16,\n\t\t\tInt32:    math.MaxInt32,\n\t\t\tInt64:    math.MaxInt64,\n\t\t\tBoolean:  false,\n\t\t\tFloat32:  math.MaxFloat32,\n\t\t\tFloat64:  math.MaxFloat64,\n\t\t\tBigInt:   *big.NewInt(math.MaxInt64),\n\t\t\tBigFloat: *big.NewFloat(math.MaxFloat64),\n\t\t}\n\n\t\t_, err := json.Marshal(d)\n\t\tif err != nil {\n\t\t\tb.Error(err)\n\t\t\tb.Fail()\n\t\t\treturn\n\t\t}\n\t}\n}\n\nfunc BenchmarkJsonUnmarshal(b *testing.B) {\n\tstr := `\n{\n  \"String\": \"\",\n  \"Time\": \"2019-10-30T16:41:29.853426+07:00\",\n  \"Int\": 2147483647,\n  \"Int8\": 127,\n  \"Int16\": 32767,\n  \"Int32\": 2147483647,\n  \"Int64\": 9223372036854775807,\n  \"Boolean\": false,\n  \"Float32\": 3.4028235e+38,\n  \"Float64\": 1.7976931348623157e+308,\n  \"BigInt\": 9999999999999999999,\n  \"BigFloat\": \"2.7976931348623157e+308\"\n}\n`\n\n\tfor n := 0; n \u003c b.N; n++ {\n\t\tvar d Data\n\t\terr := json.Unmarshal([]byte(str), \u0026d)\n\t\tif err != nil {\n\t\t\tb.Error(err)\n\t\t\tb.Fail()\n\t\t\treturn\n\t\t}\n\t}\n}\n```\n\n```\n$ go test -bench . -benchmem\ngoos: darwin\ngoarch: arm64\ncpu: Apple M2 Max\nBenchmarkJsonMarshal-12      \t 1706988\t       682.9 ns/op\t     480 B/op\t       5 allocs/op\nBenchmarkJsonUnmarshal-12    \t  345273\t      3524 ns/op\t    1816 B/op\t      27 allocs/op\nPASS\nok  \t_/Users/simonwaldherr/git/golang-benchmarks/json\t3.090s\n```\n\n### math\n\n```go\n// Package math compares the speed of various mathematical operations.\npackage math\n\nimport (\n\t\"sync\"\n\t\"sync/atomic\"\n\t\"testing\"\n)\n\nfunc BenchmarkMathInt8(b *testing.B) {\n\tvar intVal int8\n\tfor n := 0; n \u003c b.N; n++ {\n\t\tintVal = intVal + 2\n\t}\n}\n\nfunc BenchmarkMathInt32(b *testing.B) {\n\tvar intVal int32\n\tfor n := 0; n \u003c b.N; n++ {\n\t\tintVal = intVal + 2\n\t}\n}\n\nfunc BenchmarkMathInt64(b *testing.B) {\n\tvar intVal int64\n\tfor n := 0; n \u003c b.N; n++ {\n\t\tintVal = intVal + 2\n\t}\n}\n\nfunc BenchmarkMathAtomicInt32(b *testing.B) {\n\tvar intVal int32\n\tfor n := 0; n \u003c b.N; n++ {\n\t\tatomic.AddInt32(\u0026intVal, 2)\n\t}\n}\n\nfunc BenchmarkMathAtomicInt64(b *testing.B) {\n\tvar intVal int64\n\tfor n := 0; n \u003c b.N; n++ {\n\t\tatomic.AddInt64(\u0026intVal, 2)\n\t}\n}\n\ntype IntMutex struct {\n\tv   int64\n\tmux sync.Mutex\n}\n\nfunc BenchmarkMathMutexInt(b *testing.B) {\n\tvar m IntMutex\n\tfor n := 0; n \u003c b.N; n++ {\n\t\tm.mux.Lock()\n\t\tm.v = m.v + 2\n\t\tm.mux.Unlock()\n\t}\n}\n\nfunc BenchmarkMathFloat32(b *testing.B) {\n\tvar floatVal float32\n\tfor n := 0; n \u003c b.N; n++ {\n\t\tfloatVal = floatVal + 2\n\t}\n}\n\nfunc BenchmarkMathFloat64(b *testing.B) {\n\tvar floatVal float64\n\tfor n := 0; n \u003c b.N; n++ {\n\t\tfloatVal = floatVal + 2\n\t}\n}\n```\n\n```\n$ go test -bench . -benchmem\ngoos: darwin\ngoarch: arm64\ncpu: Apple M2 Max\nBenchmarkMathInt8-12           \t1000000000\t         0.2997 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkMathInt32-12          \t1000000000\t         0.2924 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkMathInt64-12          \t1000000000\t         0.2931 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkMathAtomicInt32-12    \t301260744\t         3.952 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkMathAtomicInt64-12    \t302653736\t         3.933 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkMathMutexInt-12       \t153756913\t         7.787 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkMathFloat32-12        \t1000000000\t         0.2898 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkMathFloat64-12        \t1000000000\t         0.2932 ns/op\t       0 B/op\t       0 allocs/op\nPASS\nok  \t_/Users/simonwaldherr/git/golang-benchmarks/math\t6.989s\n```\n\n### parse\n\n```go\n// Package parse benchmarks parsing.\npackage parse\n\nimport (\n\t\"strconv\"\n\t\"testing\"\n)\n\nfunc BenchmarkParseBool(b *testing.B) {\n\tfor n := 0; n \u003c b.N; n++ {\n\t\t_, err := strconv.ParseBool(\"true\")\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t}\n}\n\nfunc BenchmarkParseInt(b *testing.B) {\n\tfor n := 0; n \u003c b.N; n++ {\n\t\t_, err := strconv.ParseInt(\"1337\", 10, 64)\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t}\n}\n\nfunc BenchmarkParseFloat(b *testing.B) {\n\tfor n := 0; n \u003c b.N; n++ {\n\t\t_, err := strconv.ParseFloat(\"3.141592653589793238462643383\", 64)\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t}\n}\n```\n\n```\n$ go test -bench . -benchmem\ngoos: darwin\ngoarch: arm64\ncpu: Apple M2 Max\nBenchmarkParseBool-12     \t1000000000\t         0.2965 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkParseInt-12      \t122392257\t         9.782 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkParseFloat-12    \t20293665\t        58.70 ns/op\t       0 B/op\t       0 allocs/op\nPASS\nok  \t_/Users/simonwaldherr/git/golang-benchmarks/parse\t3.983s\n```\n\n### random\n\n```go\n// Package random compares math/rand with crypto/rand.\n// math/rand is much faster than crypto/rand, but it\n// returns only a pseudo random number.\npackage random\n\nimport (\n\tcrand \"crypto/rand\"\n\t\"encoding/base64\"\n\t\"math/big\"\n\tmrand \"math/rand\"\n\t\"testing\"\n)\n\nfunc BenchmarkMathRand(b *testing.B) {\n\tfor n := 0; n \u003c b.N; n++ {\n\t\tmrand.Int63n(0xFFFF)\n\t}\n}\n\nfunc BenchmarkCryptoRand(b *testing.B) {\n\tfor n := 0; n \u003c b.N; n++ {\n\t\t_, err := crand.Int(crand.Reader, big.NewInt(0xFFFF))\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t}\n}\n\nfunc BenchmarkCryptoRandString(b *testing.B) {\n\tfor n := 0; n \u003c b.N; n++ {\n\t\t_, err := GenerateRandomString(32)\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t}\n}\n\nfunc BenchmarkCryptoRandBytes(b *testing.B) {\n\tfor n := 0; n \u003c b.N; n++ {\n\t\t_, err := GenerateRandomBytes(32)\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t}\n}\n\nfunc GenerateRandomBytes(n int) ([]byte, error) {\n\tb := make([]byte, n)\n\t_, err := mrand.Read(b)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\treturn b, nil\n}\n\nfunc GenerateRandomString(s int) (string, error) {\n\tb, err := GenerateRandomBytes(s)\n\treturn base64.URLEncoding.EncodeToString(b), err\n}\n```\n\n```\n$ go test -bench . -benchmem\ngoos: darwin\ngoarch: arm64\ncpu: Apple M2 Max\nBenchmarkMathRand-12            \t172330776\t         6.747 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkCryptoRand-12          \t 9851932\t       121.8 ns/op\t      48 B/op\t       3 allocs/op\nBenchmarkCryptoRandString-12    \t11226271\t       108.5 ns/op\t     128 B/op\t       3 allocs/op\nBenchmarkCryptoRandBytes-12     \t20358463\t        58.10 ns/op\t      32 B/op\t       1 allocs/op\nPASS\nok  \t_/Users/simonwaldherr/git/golang-benchmarks/random\t5.720s\n```\n\n### regexp\n\n```go\n// Package regexp benchmarks the performance of a pre-compiled regexp match\n// a non-pre-compiled match and JIT-cached-compilation via golibs: https://simonwaldherr.de/go/golibs\npackage regexp\n\nimport (\n\t\"regexp\"\n\t\"testing\"\n\n\t\"simonwaldherr.de/go/golibs/regex\"\n)\n\nvar regexpStr string = `^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,9}$`\n\nfunc BenchmarkMatchString(b *testing.B) {\n\tfor n := 0; n \u003c b.N; n++ {\n\t\t_, err := regexp.MatchString(regexpStr, \"john.doe@example.tld\")\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t}\n}\n\nfunc BenchmarkMatchStringCompiled(b *testing.B) {\n\tr, err := regexp.Compile(regexpStr)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tb.ResetTimer()\n\tfor n := 0; n \u003c b.N; n++ {\n\t\tr.MatchString(\"john.doe@example.tld\")\n\t}\n}\n\nfunc BenchmarkMatchStringGolibs(b *testing.B) {\n\tfor n := 0; n \u003c b.N; n++ {\n\t\t_, err := regex.MatchString(\"john.doe@example.tld\", regexpStr)\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t}\n}\n```\n\n```\n$ go test -bench . -benchmem\ngoos: darwin\ngoarch: arm64\ncpu: Apple M2 Max\nBenchmarkMatchString-12            \t  318897\t      3804 ns/op\t   10223 B/op\t      86 allocs/op\nBenchmarkMatchStringCompiled-12    \t 3770263\t       325.7 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkMatchStringGolibs-12      \t 3669634\t       326.9 ns/op\t       0 B/op\t       0 allocs/op\nPASS\nok  \t_/Users/simonwaldherr/git/golang-benchmarks/regexp\t5.265s\n```\n\n### template\n\n```go\n// Package template benchmarks the performance of different templating methods\npackage template\n\nimport (\n\t\"bytes\"\n\thtmltemplate \"html/template\"\n\t\"regexp\"\n\t\"testing\"\n\ttexttemplate \"text/template\"\n)\n\n// Define a struct to hold the data for the templates\ntype Data struct {\n\tName    string\n\tAddress string\n}\n\n// Prepare the templates and data\nvar (\n\tdata = Data{Name: \"John Doe\", Address: \"123 Elm St\"}\n\n\ttextTplString = \"Name: {{.Name}}, Address: {{.Address}}\"\n\thtmlTplString = \"\u003cdiv\u003eName: {{.Name}}\u003c/div\u003e\u003cdiv\u003eAddress: {{.Address}}\u003c/div\u003e\"\n\tregExpString  = \"Name: {{NAME}}, Address: {{ADDRESS}}\"\n)\n\n// Benchmark for text/template\nfunc BenchmarkTextTemplate(b *testing.B) {\n\ttpl, _ := texttemplate.New(\"text\").Parse(textTplString)\n\tb.ResetTimer()\n\tfor i := 0; i \u003c b.N; i++ {\n\t\tvar buf bytes.Buffer\n\t\ttpl.Execute(\u0026buf, data)\n\t}\n}\n\n// Benchmark for html/template\nfunc BenchmarkHTMLTemplate(b *testing.B) {\n\ttpl, _ := htmltemplate.New(\"html\").Parse(htmlTplString)\n\tb.ResetTimer()\n\tfor i := 0; i \u003c b.N; i++ {\n\t\tvar buf bytes.Buffer\n\t\ttpl.Execute(\u0026buf, data)\n\t}\n}\n\n// Benchmark for replacing placeholders using regexp\nfunc BenchmarkRegExp(b *testing.B) {\n\trName := regexp.MustCompile(`{{NAME}}`)\n\trAddress := regexp.MustCompile(`{{ADDRESS}}`)\n\tb.ResetTimer()\n\tfor i := 0; i \u003c b.N; i++ {\n\t\tresult := rName.ReplaceAllString(regExpString, data.Name)\n\t\tresult = rAddress.ReplaceAllString(result, data.Address)\n\t}\n}\n```\n\n```\n$ go test -bench . -benchmem\ngoos: darwin\ngoarch: arm64\ncpu: Apple M2 Max\nBenchmarkTextTemplate-12    \t 3461511\t       345.5 ns/op\t     272 B/op\t       5 allocs/op\nBenchmarkHTMLTemplate-12    \t 1000000\t      1165 ns/op\t     592 B/op\t      19 allocs/op\nBenchmarkRegExp-12          \t 2807174\t       417.6 ns/op\t     298 B/op\t       9 allocs/op\nPASS\nok  \t_/Users/simonwaldherr/git/golang-benchmarks/template\t4.518s\n```\n\n### trim\n\n```go\n```\n\n```\n$ go test -bench . -benchmem\nFAIL\t_/Users/simonwaldherr/git/golang-benchmarks/trim [setup failed]\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimonwaldherr%2Fgolang-benchmarks","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsimonwaldherr%2Fgolang-benchmarks","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimonwaldherr%2Fgolang-benchmarks/lists"}