Created
August 28, 2019 10:55
-
-
Save rust-play/f732f0e9d25931d27545978e8d9b8421 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
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
use std::time::{ Instant, Duration }; | |
fn benchmark<F: FnOnce()>(f: F) { | |
let t0 = Instant::now(); | |
f(); | |
let t1 = Instant::now(); | |
let dt = t1 - t0; | |
println!("{}", dt.as_millis()); | |
} | |
fn main() { | |
let a: Vec<_> = (0..10000000).into_iter().collect(); | |
let mut b = a.clone(); | |
benchmark(move || { | |
let c: Vec<_> = a.into_iter().filter(|&x| x % 2 == 0).collect(); | |
println!("{}", c.len()); | |
}); | |
benchmark(|| { | |
b.retain(|&x| x % 2 == 0); | |
println!("{}", b.len()); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment