Idiomatic way to measure CPU time

What is an idiomatic way in Rust to measure, in CPU time, how long a function takes to execute?

For wall-clock time, I measure a duration like this:

use std::time::Instant;

fn interesting_function() { /* stuff that takes some time */ }

fn main() {
  let start_time = Instant::now();
  interesting_function();
  let duration = start_time.elapsed();
  println!("{}", duration.as_secs_f64())
}

While now() calls clock_gettime with CLOCK_MONOTONIC, I believe that to measure CPU time I would want CLOCK_PROCESS_CPUTIME_ID to be used instead.

Use libc directly for that. Here is an example:

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.