Created
April 25, 2024 00:07
-
-
Save rektdeckard/28e055e70e859914a398d71ef006c77f to your computer and use it in GitHub Desktop.
Time Async Rust Functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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