Skip to content

Instantly share code, notes, and snippets.

@oconnor663
Created December 22, 2019 02:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oconnor663/06206df07fa48457901220008b66cb8b to your computer and use it in GitHub Desktop.
Save oconnor663/06206df07fa48457901220008b66cb8b to your computer and use it in GitHub Desktop.
sampling what rdtsc reports for the clock frequency (often wrong)
fn ticks() -> u64 {
let mut discard = 0;
unsafe { core::arch::x86_64::__rdtscp(&mut discard) }
}
fn main() {
let t0 = std::time::Instant::now();
let c0 = ticks();
std::thread::sleep(std::time::Duration::from_secs(1));
let t1 = std::time::Instant::now();
let c1 = ticks();
let t_diff = t1 - t0;
let c_diff = c1 - c0;
dbg!(t_diff);
dbg!(c_diff);
let ghz = c_diff as f64 / t_diff.as_nanos() as f64;
dbg!(ghz);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment