{"id":13699648,"url":"https://github.com/qi7chen/timer-benchmarks","last_synced_at":"2026-02-05T06:32:48.824Z","repository":{"id":145397875,"uuid":"120721911","full_name":"qi7chen/timer-benchmarks","owner":"qi7chen","description":"Benchmark of different timer implementations(min-heap, red-black tree, timing wheel) 不同数据结构实现的定时器测试","archived":false,"fork":false,"pushed_at":"2024-02-01T06:37:06.000Z","size":2754,"stargazers_count":152,"open_issues_count":5,"forks_count":44,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-10-16T06:48:43.038Z","etag":null,"topics":["cplusplus","minheap","redblacktree","timer","timewheel"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/qi7chen.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"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}},"created_at":"2018-02-08T06:40:43.000Z","updated_at":"2025-08-19T01:47:42.000Z","dependencies_parsed_at":"2024-12-14T08:03:45.846Z","dependency_job_id":"324e272d-eb00-42e3-bee0-d5a280b3bdc5","html_url":"https://github.com/qi7chen/timer-benchmarks","commit_stats":null,"previous_names":["qi7chen/timer-benchmarks","qki7chen/timer-benchmarks","k7tchen/timer-benchmarks","ki7chen/timer-benchmarks","qchencc/timer-benchmarks"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/qi7chen/timer-benchmarks","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qi7chen%2Ftimer-benchmarks","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qi7chen%2Ftimer-benchmarks/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qi7chen%2Ftimer-benchmarks/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qi7chen%2Ftimer-benchmarks/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/qi7chen","download_url":"https://codeload.github.com/qi7chen/timer-benchmarks/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qi7chen%2Ftimer-benchmarks/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29114910,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-05T05:31:32.482Z","status":"ssl_error","status_checked_at":"2026-02-05T05:31:29.075Z","response_time":65,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":["cplusplus","minheap","redblacktree","timer","timewheel"],"created_at":"2024-08-02T20:00:40.327Z","updated_at":"2026-02-05T06:32:48.798Z","avatar_url":"https://github.com/qi7chen.png","language":"C++","readme":"# timer-benchmark\n\n测试不同的数据结构（最小堆、四叉堆、红黑树、时间轮）实现的定时器的性能差异。\n\nas [Hashed and Hierarchical Timing Wheels](http://www.cs.columbia.edu/~nahum/w6998/papers/sosp87-timing-wheels.pdf) implies\n\na timer module has 3 component routines:\n\n``` C++\n// start a timer that will expire after `interval` unit of time\n// return an unique id of the pending timer\nint Start(interval, expiry_action)\n\n// cancel a timer identified by `timer_id`\nvoid Cancel(timer_id)\n\n// per-tick bookking routine\n// in single-thread timer scheduler implementions, this routine will run timeout actions\nint Update(now)\n```\n\nuse [min-heap](https://en.wikipedia.org/wiki/Heap_(data_structure)), quaternary heap( [4-ary heap](https://en.wikipedia.org/wiki/D-ary_heap) ),\nbalanced binary search tree( [red-black tree](https://en.wikipedia.org/wiki/Red-black_tree) ), [hashed timing wheel](https://netty.io/4.0/api/io/netty/util/HashedWheelTimer.html)\nand [Hierarchical timing wheel](https://lwn.net/Articles/646950/) to implement different time scheduler.\n\n\n## Big(O) complexity of algorithm\n\nFIFO means whether same deadline timers expire in FIFO order.\n\nFIFO的意思是相同到期时间的定时器是否按FIFO的顺序到期\n\nalgo                      |          | Start()  | Cancel() | Tick()   |  FIFO  | implemention file\n--------------------------|----------|----------|----------|----------|--------|-----------------------\nbinary heap               | 最小堆   | O(log N) | O(log N) | O(1)     |   no   | [PriorityQueueTimer](src/PriorityQueueTimer.h)\n4-ary heap                | 四叉堆   | O(log N) | O(log N) | O(1)     |   no   | [QuatHeapTimer](src/QuatHeapTimer.h)\nredblack tree             | 红黑树   | O(log N) | O(log N) | O(log N) |   no   | [RBTreeTimer](src/RBTreeTimer.h)\nhashed timing wheel       | 时间轮   | O(1)     | O(1)     | O(1)     |   yes  | [HashedWheelTimer](src/HashedWheelTimer.h)\nhierarchical timing wheel | 多级时间轮 | O(1)   | O(1)     | O(1)     |   yes  | [HHWheelTimer](src/HHWheelTimer.h)\n\n\n## How To Build\n\n### Obtain CMake\n\nObtain [CMake](https://cmake.org) first.\n\n* `sudo apt install cmake` on Ubuntu or Debian\n* `sudo yum install cmake` on Redhat or CentOS\n* `choco install cmake` on Windows use [choco](https://chocolatey.org/)\n\nrun shell command\n\n* `mkdir cmake-build; cd cmake-build \u0026\u0026 cmake -DCMAKE_BUILD_TYPE=Release .. \u0026\u0026 cmake --build .`\n\n\n\n## Benchmarks\n\n## Benchmark result\n\nWin10 x64 6-core 3.93MHz CPU\n\n\nBenchmark               |        Time     |       CPU  | Iterations\n------------------------|-----------------|------------|---------------\nBM_PQTimerAdd           |      441 ns     |    433 ns  |   1947826\nBM_QuadHeapTimerAdd     |      429 ns     |    427 ns  |   1866667\nBM_RBTreeTimerAdd       |     1231 ns     |   1228 ns  |   1120000\nBM_HashWheelTimerAdd    |      430 ns     |    436 ns  |   1792000\nBM_HHWheelTimerAdd      |      669 ns     |    672 ns  |   1000000\nBM_PQTimerCancel        |      668 ns     |    656 ns  |   1000000\nBM_QuadHeapTimerCancel  |      351 ns     |    349 ns  |   2240000\nBM_RBTreeTimerCancel    |     1685 ns     |   1692 ns  |    896000\nBM_HashWheelTimerCancel |      632 ns     |    641 ns  |   1000000\nBM_HHWheelTimerCancel   |      942 ns     |    953 ns  |   1000000\nBM_PQTimerTick          |     29.8 ns     |   29.8 ns  |  23578947\nBM_QuadHeapTimerTick    |     30.3 ns     |   30.5 ns  |  23578947\nBM_RBTreeTimerTick      |     30.2 ns     |   29.8 ns  |  23578947\nBM_HashWheelTimerTick   |     31.2 ns     |   30.8 ns  |  21333333\nBM_HHWheelTimerTick     |     30.5 ns     |   30.7 ns  |  22400000\n\n## Conclusion\n\n* rbtree timer Add/Cancel has not so good performance compare to other implementations;\n* 红黑树的插入和删除相比其它实现，表现都弱了一些；\n* binary min heap is a good choice, easy to implement and have a good performance, but without FIFO expiration order(heap sort is unstable);\n* 最小堆是一个不错的选择，代码实现简单性能也不俗，但不支持相同超时的定时器按FIFO顺序触发;\n\n\n## Reference\n\n* [Hashed and Hierarchical Timing Wheels](https://paulcavallaro.com/blog/hashed-and-hierarchical-timing-wheels/)\n* [Hashed and Hierarchical Timing Wheels](http://www.cs.columbia.edu/~nahum/w6998/papers/sosp87-timing-wheels.pdf)\n* [Netty HashedWheelTimer](https://github.com/netty/netty/blob/4.1/common/src/main/java/io/netty/util/HashedWheelTimer.java)\n* [Apache Kafka, Purgatory, and Hierarchical Timing Wheels](https://www.confluent.io/blog/apache-kafka-purgatory-hierarchical-timing-wheels/s)\n\n\n","funding_links":[],"categories":["C++"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqi7chen%2Ftimer-benchmarks","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fqi7chen%2Ftimer-benchmarks","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqi7chen%2Ftimer-benchmarks/lists"}