{"id":16825012,"url":"https://github.com/tomnomnom/branchdemo","last_synced_at":"2025-07-22T08:04:58.223Z","repository":{"id":34011321,"uuid":"37767540","full_name":"tomnomnom/branchdemo","owner":"tomnomnom","description":"CPU Branch Predictor Demo","archived":false,"fork":false,"pushed_at":"2015-06-20T21:36:32.000Z","size":124,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-15T04:45:47.513Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tomnomnom.png","metadata":{"files":{"readme":"README.mkd","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-06-20T11:25:14.000Z","updated_at":"2025-02-18T20:12:00.000Z","dependencies_parsed_at":"2022-07-13T21:53:26.342Z","dependency_job_id":null,"html_url":"https://github.com/tomnomnom/branchdemo","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/tomnomnom/branchdemo","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomnomnom%2Fbranchdemo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomnomnom%2Fbranchdemo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomnomnom%2Fbranchdemo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomnomnom%2Fbranchdemo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tomnomnom","download_url":"https://codeload.github.com/tomnomnom/branchdemo/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomnomnom%2Fbranchdemo/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266454946,"owners_count":23931349,"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","status":"online","status_checked_at":"2025-07-22T02:00:09.085Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":[],"created_at":"2024-10-13T11:12:40.143Z","updated_at":"2025-07-22T08:04:58.198Z","avatar_url":"https://github.com/tomnomnom.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Branch Predictor Demo\n\nSome example code to demonstrate the effects a CPU's [branch predictor](https://en.wikipedia.org/wiki/Branch_predictor) can\nhave on performance.\n\nThe example function counts the occurences of an integer in a slice of integers.\n\n```go\nfunc CountInt(search int, ints []int) int {\n\tcount := 0\n\n\tfor _, num := range ints {\n\t\tif num == search {\n\t\t\tcount++\n\t\t}\n\t}\n\n\treturn count\n}\n```\n\nWhen the provided integers are unsorted, the CPU's branch predictor is unable to reliably predict if the `num == search`\nbranch of the code will be followed on each iteration, incurring a penalty each time the prediction is wrong.\nHowever, when the integers are sorted: every occurrence of the `num == search` branch being followed will happen consecutively,\nallowing the branch predictor to be correct more often.\n\nThe included benchmarks count the occurences of the number 7 in a slice of 10,000 random integers between 0 and 100.\n\nNote that the 'sorted' benchmark includes the time taken to sort the integers (once), and is therefore doing slightly more work.\n\n\n## Example Benchmark Runs\n\nIntel(R) Core(TM) i7-2670QM CPU @ 2.20GHz; go version go1.4.2 linux/amd64 (Laptop):\n\n    tom@megatron:~/src/github.com/tomnomnom/branchdemo (master)▶ go test -bench .\n    PASS\n    BenchmarkCountIntUnsorted  200000      8874 ns/op\n    BenchmarkCountIntSorted    200000      6778 ns/op\n    ok      github.com/tomnomnom/branchdemo 3.301s\n\nIntel(R) Xeon(R) CPU E5-2630L v2 @ 2.40GHz; go version go1.4.2 linux/amd64 (Digital Ocean, $5/mo instance):\n\n    tom@sh:~/src/github.com/tomnomnom/branchdemo (master)▶ go test -bench .\n    PASS\n    BenchmarkCountIntUnsorted  200000      10605 ns/op\n    BenchmarkCountIntSorted    200000      7926 ns/op\n    ok      github.com/tomnomnom/branchdemo 3.907s\n\nAMD Turion(tm) II Neo N54L Dual-Core Processor; go version go1.4.2 linux/amd64 (HP Proliant N54L):\n\n    tom@nabu:~/src/github.com/tomnomnom/branchdemo (master)▶ go test -bench .\n    PASS\n    BenchmarkCountIntUnsorted  100000      23593 ns/op\n    BenchmarkCountIntSorted    100000      22782 ns/op\n    ok      github.com/tomnomnom/branchdemo 5.131s\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomnomnom%2Fbranchdemo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftomnomnom%2Fbranchdemo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomnomnom%2Fbranchdemo/lists"}