Skip to content

Instantly share code, notes, and snippets.

@matthewjberger
Last active January 7, 2023 07:22
Embed
What would you like to do?
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