Skip to content

Instantly share code, notes, and snippets.

@rektdeckard
Created April 25, 2024 00:07
Show Gist options
  • Save rektdeckard/28e055e70e859914a398d71ef006c77f to your computer and use it in GitHub Desktop.
Save rektdeckard/28e055e70e859914a398d71ef006c77f to your computer and use it in GitHub Desktop.
Time Async Rust Functions
async fn time_async<F, O>(f: F) -> (O, Duration)
where
F: std::future::Future<Output = O>,
{
let start = std::time::Instant::now();
let out = f.await;
let duration = start.elapsed();
(out, duration)
}
async fn log_time_async<F, O>(f: F) -> O
where
F: std::future::Future<Output = O>,
O: Debug,
{
let (out, duration) = time_async(f).await;
dbg!(&out, duration);
out
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment