Skip to content

Instantly share code, notes, and snippets.

@matthewjberger
Last active January 7, 2023 07:22
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 matthewjberger/d6d2124ab51e460ae3e380bb1f26e5c3 to your computer and use it in GitHub Desktop.
Save matthewjberger/d6d2124ab51e460ae3e380bb1f26e5c3 to your computer and use it in GitHub Desktop.
A rust profiling macro
// let (entities, duration) = profile!(world.create_entities(1_000_000));
// println!("Creating 1 million entities completed in {:?}", duration);
//
// let results: Vec<(_, Duration)> = profile_n!(100, world.create_entities(1_000_000));
#[macro_export]
macro_rules! profile {
($($actions:tt)*) => {
{
let start = std::time::Instant::now();
let result = $($actions)*;
let duration = start.elapsed();
(result, duration)
}
}
}
#[macro_export]
macro_rules! profile_n {
($n:tt, $($actions:tt)*) => {
(0..$n).map(|_| {
profile!($($actions)*)
})
.collect::<Vec<_>>()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment