1 //! This module contains a few utilities to measure how long executing algorithms takes.
2 //! It utilizes the `time` crate.
4 /// This function will measure how long it takes to execute the given lambda,
5 /// print the time and return the result of the lambda.
6 pub fn report_time<Out, F: FnOnce() -> Out>(name: &str, f: F) -> Out {
7 let start = time::now();
8 eprintln!("starting {}", name);
10 eprintln!("done {} - took: {}", name, (time::now() - start));
14 /// This function will measure how long it takes to execute the given lambda
15 /// and return a tuple of the result of the lambda and a duration object.
16 pub fn measure<Out, F: FnOnce() -> Out>(f: F) -> (Out, time::Duration) {
17 let start = time::now();
19 (res, time::now() - start)
22 /// A struct to repeatedly measure the time passed since the timer was started
28 impl Default for Timer {
29 fn default() -> Self {
35 /// Create and start a new `Timer`
36 pub fn new() -> Timer {
37 Timer { start: time::now() }
41 pub fn restart(&mut self) {
42 self.start = time::now();
45 /// Print the passed time in ms since the timer was started
46 pub fn report_passed_ms(&self) {
47 eprintln!("{}ms", (time::now() - self.start).num_milliseconds());
50 /// Return the number of ms passed since the timer was started as a `i64`
51 pub fn get_passed_ms(&self) -> i64 {
52 (time::now() - self.start).num_milliseconds()