{"id":20453474,"url":"https://github.com/sheroz/tick_counter","last_synced_at":"2025-07-26T15:11:39.992Z","repository":{"id":177439107,"uuid":"660386424","full_name":"sheroz/tick_counter","owner":"sheroz","description":"Hardware-based tick counters for high-precision benchmarks in Rust","archived":false,"fork":false,"pushed_at":"2024-05-27T22:05:38.000Z","size":53,"stargazers_count":28,"open_issues_count":1,"forks_count":6,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-25T18:28:59.000Z","etag":null,"topics":["benchmarking","clock","counter","rdtsc","rdtscp","rust","time-stamp-counter","timer","timer-clock"],"latest_commit_sha":null,"homepage":"https://sheroz.com/pages/blog/rust-tick-counter.html","language":"Rust","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/sheroz.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","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,"zenodo":null}},"created_at":"2023-06-29T22:32:19.000Z","updated_at":"2025-05-22T15:41:59.000Z","dependencies_parsed_at":"2025-04-13T03:38:13.278Z","dependency_job_id":"c4ec072a-f4d4-4c62-acd8-6d8250a7aa8c","html_url":"https://github.com/sheroz/tick_counter","commit_stats":{"total_commits":57,"total_committers":4,"mean_commits":14.25,"dds":0.4385964912280702,"last_synced_commit":"b15ab822909c1304caf5087c5b42c0cd4eab2662"},"previous_names":["sheroz/cpu_counter","sheroz/tick_counter"],"tags_count":14,"template":false,"template_full_name":null,"purl":"pkg:github/sheroz/tick_counter","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sheroz%2Ftick_counter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sheroz%2Ftick_counter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sheroz%2Ftick_counter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sheroz%2Ftick_counter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sheroz","download_url":"https://codeload.github.com/sheroz/tick_counter/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sheroz%2Ftick_counter/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267184092,"owners_count":24049127,"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-26T02:00:08.937Z","response_time":62,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","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":["benchmarking","clock","counter","rdtsc","rdtscp","rust","time-stamp-counter","timer","timer-clock"],"created_at":"2024-11-15T11:12:55.663Z","updated_at":"2025-07-26T15:11:39.947Z","avatar_url":"https://github.com/sheroz.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Hardware-based tick counters for high-precision benchmarks\n\n[![crates.io](https://img.shields.io/crates/v/tick_counter)](https://crates.io/crates/tick_counter)\n[![docs](https://img.shields.io/docsrs/tick_counter)](https://docs.rs/tick_counter/latest/tick_counter/)\n[![build \u0026 test](https://github.com/sheroz/tick_counter/actions/workflows/ci.yml/badge.svg)](https://github.com/sheroz/tick_counter/actions/workflows/ci.yml)\n[![license](https://img.shields.io/github/license/sheroz/tick_counter)](https://github.com/sheroz/tick_counter/blob/main/LICENSE.txt)\n\n- x86_64: executes [RDTSC](https://www.intel.com/content/dam/www/public/us/en/documents/white-papers/ia-32-ia-64-benchmark-code-execution-paper.pdf) CPU instruction to read the time-stamp counter.\n- AArch64: reads value of the [CNTVCT_EL0](https://developer.arm.com/documentation/ddi0595/2021-12/AArch64-Registers/CNTVCT-EL0--Counter-timer-Virtual-Count-register) counter-timer register.\n\nThe brief description: [https://sheroz.com/pages/blog/rust-tick-counter.html](https://sheroz.com/pages/blog/rust-tick-counter.html)\n\n## Tested on platforms\n\n```text\nx86_64 (Intel® Core™ i7)\nAArch64 (Apple M1 Pro)\n```\n\n## Usage\n\nFor usage samples please look at [src/bin/sample.rs](src/bin/sample.rs)\n\n### Basic usage\n\n```rust\nlet start = tick_counter::start();\n// ... lines of code to benchmark\nlet elapsed_ticks = tick_counter::stop() - start;\nprintln!(\"Number of elapsed ticks: {}\", elapsed_ticks);\n```\n\n### Basic usage with helper\n\n```rust\nuse tick_counter::TickCounter;\n \nlet tick_counter = TickCounter::current();\n// ... lines of code to benchmark\nlet elapsed_ticks = tick_counter.elapsed();\nprintln!(\"Number of elapsed ticks: {}\", elapsed_ticks);\n```\n\n### Extended usage\n\n```rust\nprintln!(\"Environment: {}/{} {}\", consts::OS, consts::FAMILY, consts::ARCH);\n\nlet (counter_frequency, accuracy) = tick_counter::frequency();\nlet frequency_base = match accuracy {\n    tick_counter::TickCounterFrequencyBase::Hardware =\u003e \"hardware provided\".to_string(),\n    tick_counter::TickCounterFrequencyBase::Measured(duration) =\u003e format!(\"software estimated in {:?}\", duration)\n};\nprintln!(\"Tick frequency, MHZ: {:.2} ({})\", counter_frequency as f64 / 1e6_f64, frequency_base);\n\nlet counter_accuracy = tick_counter::precision_nanoseconds(counter_frequency);\nprintln!(\"Tick accuracy, nanoseconds: {:.2}\", counter_accuracy);\n\nlet counter_start = tick_counter::start();\nthread::sleep(time::Duration::from_secs(1));\nlet counter_stop = tick_counter::stop();\n\nprintln!(\"Tick counter start: {}\", counter_start);\nprintln!(\"Tick counter stop: {}\", counter_stop);\n\nlet elapsed_ticks = counter_stop - counter_start;\nprintln!(\"Elapsed ticks count in 1 seconds: {}\", elapsed_ticks);\n\nlet elapsed_nanoseconds = (elapsed_ticks as f64) * counter_accuracy;\nprintln!(\"Elapsed nanoseconds according to elapsed ticks: {:.2}\", elapsed_nanoseconds);\n```\n\n### Outputs\n\n#### 1. Macbook Pro 16 2021 / Apple Silicon\n\n```text\nApple M1 Pro\nMacOS Ventura 13.5.1, Darwin Kernel Version 22.6.0\n```\n\nOutput\n\n```text\nBasic usage:\nNumber of elapsed ticks in 1s: 24120997\n\nBasic usage with helper:\nNumber of elapsed ticks in 1s: 24122097\n\nExtended usage:\nEnvironment: macos/unix aarch64\nTick frequency, MHZ: 24.00 (hardware provided)\nTick accuracy, nanoseconds: 41.67\nTick counter start: 103684134140\nTick counter stop: 103708255194\nElapsed ticks count in 1 seconds: 24121054\nElapsed nanoseconds according to elapsed ticks: 1005043916.67\n\nComparing the measurement methods using 100 samples:\nElapsed time in nanoseconds, using std::time::Instant\n  Mean = 60.34\n  Min  = 41.00\n  Max  = 167.00\n  Standard deviation = 23.92 (39.64 %)\nElapsed time in nanoseconds, using tick_counter\n  Mean = 42.41\n  Min  = 42.00\n  Max  = 83.00\n  Standard deviation = 4.08 (9.62 %)\n```\n\n#### 2. Ubuntu 22.04 LTS / Intel® Core™ i7\n\n```text\nIntel(R) Core(TM) i7-3770 CPU @ 3.40GHz\nLinux 6.2.0-31-generic #31~22.04.1-Ubuntu\n```\n\nOutput\n\n```text\nBasic usage:\nNumber of elapsed ticks in 1s: 3430864159\n\nBasic usage with helper:\nNumber of elapsed ticks in 1s: 3430866807\n\nExtended usage:\nEnvironment: linux/unix x86_64\nTick frequency, MHZ: 3430.87 (software estimated in 1s)\nTick accuracy, nanoseconds: 0.29\nTick counter start: 14959335168548\nTick counter stop: 14962766085156\nElapsed ticks count in 1 seconds: 3430916608\nElapsed nanoseconds according to elapsed ticks: 1000013467.72\n\nComparing results, using 100 samples:\nElapsed time in nanoseconds, using std::time::Instant\n  Mean = 46.35\n  Min  = 42.00\n  Max  = 262.00\n  Standard deviation = 21.70 (46.82 %)\nElapsed time in nanoseconds, using tick_counter\n  Mean = 17.40\n  Min  = 15.00\n  Max  = 18.00\n  Standard deviation = 0.95 (5.45 %)\n```\n\n## Disclaimer\n\nThis project was created for research purposes and is not intended for use in production systems.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsheroz%2Ftick_counter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsheroz%2Ftick_counter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsheroz%2Ftick_counter/lists"}