Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created August 28, 2019 10:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rust-play/f732f0e9d25931d27545978e8d9b8421 to your computer and use it in GitHub Desktop.
Save rust-play/f732f0e9d25931d27545978e8d9b8421 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
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