ohsayan/devtimer

The compact yet complete benchmarking suite for Rust

This project is licensed under the Apache-2

Devtimer

The compact yet complete benchmarking suite for Rust. Period.

Rationale

I've seen many, many benchmarking tools. However, no one realizes that we need simplicity to simplify development and increase productivity. devtimer provides a very compact yet complete benchmarking suite for code written in Rust. It makes use of the standard library only to provide benchmark operations. You can either use it for benchmarking a single operation or you can use it for running an operation multiple times and finding the min, max and average execution times. Since this crate has no external dependencies, it is small, fast and does exactly what it claims to. Happy benchmarking!

Need help migrating from an older version? See the changelog.

Usage

Add this to your cargo.toml :

devtimer = "*"

Then add this line to your source file (i.e main.rs or lib.rs or where you need to use it):

use devtimer::DevTime;

Example usage

Simple usage

Let's say there are two functions called very_long_operation() and another_op() that take a very long time to execute. Then we can time it's execution as shown below:

fn main() {
    let mut timer = DevTime::new_simple();
    timer.start();
    very_long_operation();
    timer.stop();
    println!("The operation took: {} ns", timer.time_in_nanos().unwrap());
    // You can keep re-using the timer for other operations
    timer.start(); // this resets the timer and starts it again
    another_op();
    timer.stop();
    println!("The operation took: {} secs", timer.time_in_secs().unwrap());
    println!("The operation took: {} milliseconds", timer.time_in_millis().unwrap());
    println!("The operation took: {} microseconds", timer.time_in_micros().unwrap());
    println!("The operation took: {} ns", timer.time_in_nanos().unwrap());

    // With version 1.1.0 and upwards
    timer.start_after(&std::time::Duration::from_secs(2));
    // The timer will start after two seconds
    // Do some huge operation now
    timer.stop();
    println!("The operation took: {} nanoseconds", devtimer.time_in_nanos().unwrap());
}

Example: Benchmarking

use devtimer::run_benchmark;
fn main() {
  // We will simulate a long operation by std::thread::sleep()
  // Run 10 iterations for the test
  let bench_result = run_benchmark(10, |_| {
    // Fake a long running operation
    std::thread::sleep(std::time::Duration::from_secs(1);
  });
  bench_result.print_stats();
}

Advanced Benchmarking

The run_benchmark() function also provides a usize that can be used, say if you want to get something from an array to do the test. For example:

run_benchmark(100, |n| {
  do_action(data_source[n]);
});

Example: Tagged timers

use devtimer::DevTime;
fn main() {
  let mut cmplx = DevTime::new_complex();
  // Create a timer with tag `timer-1` 
  cmplx.create_timer("timer-1").unwrap();
  cmplx.start_timer("timer-1").unwrap();
  // Simulate a slow operation
  std::thread::sleep(std::time::Duration::from_secs(1));
  cmplx.stop_timer("timer-1").unwrap();
  
  // Create a timer with tag `cool-timer` 
  cmplx.create_timer("cool-timer").unwrap();
  cmplx.start_timer("cool-timer").unwrap();
  // Simulate a slow operation
  std::thread::sleep(std::time::Duration::from_secs(2));
  cmplx.stop_timer("cool-timer").unwrap();

  // We can output a benchmark in this way
  println!(" `cool-timer` took: {}", cmplx.time_in_micros("cool-timer").unwrap());

  // Or we can iterate through all timers
  for (tname, timer) in cmplx.iter() {
    println!("{} - {} ns", tname, timer.time_in_micros().unwrap());
  }

  // Or we can print results in the default '{timername} - {time} ns' format
  cmplx.print_stats();
}

Timing functions available (names are self explanatory):

  • time_in_secs() -> Returns the number of seconds the operation took
  • time_in_millis() -> Returns the number of milliseconds the operation took
  • time_in_micros() -> Returns the number of microseconds the operation took
  • time_in_nanos() -> Return the number of nanoseconds the operation took

See the full docs here.

License

This project is licensed under the Apache-2.0 License. Keep coding and benchmarking!

Versions

Find the latest versions by id

Information - Updated Jul 10, 2022

Stars: 5
Forks: 0
Issues: 0

Repositories & Extras

This is an example of a Rust server that functions as a remote schema for...

Rust + Hasura Rust server that functions as a Hasura

This is an example of a Rust server that functions as a remote schema for...

Newport Engine is a modular 2D and 3D game engine built in Rust for Rust

It is designed to be easily extendable and easy to use

Newport Engine is a modular 2D and 3D game engine built in Rust for Rust

liboqs-rust: Rust bindings for liboqs

Qyantum Safe liboqs rust bindings

liboqs-rust: Rust bindings for liboqs

msgflo-rust: Rust participant support for MsgFlo

Flowhub visual programming IDE

msgflo-rust: Rust participant support for MsgFlo
Actix

1.2K

How to be a full stack Rust Developer

Read Rust the Rust blog posts at Steadylearner

How to be a full stack Rust Developer

Rust library translation (rust-src/rust-std/stdlib/rustlib translation)

This is the place to translate Having a documentation in your native language is essential if you don't speak English, and still enjoyable even if...

Rust library translation (rust-src/rust-std/stdlib/rustlib translation)

False Positive for rust-lang/rust#83583

The deprecation lint proc_macro_derive_resolution_fallback is intended to catch proc macro generated code that refers to items from parent modules that should not be in scope:

False Positive for rust-lang/rust#83583

xbuild is a build tool for rust and rust/flutter projects with support for cross compiling...

xbuild is a build tool for rust and rust/flutter projects with support for cross compiling and

xbuild is a build tool for rust and rust/flutter projects with support for cross compiling...

Rust: setup Rust with rustup

wabt: --sysroot value in the Makefiles when using a different target localtion for wasi-sdk)

Rust: setup Rust with rustup

How to be a full stack Rust Developer

Read Rust the Rust blog posts at Steadylearner

How to be a full stack Rust Developer

Snake game developed in Rust using rust-sdl2 crate

Snake game developed in Rust using

Snake game developed in Rust using rust-sdl2 crate

This is a game engine for rust in rust

I'm building it as a learning experience to try and better understand rust and what goes into making a game engine

This is a game engine for rust in rust
Facebook Instagram Twitter GitHub Dribbble
Privacy