Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created January 14, 2020 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rust-play/3186ef3d443784bba4839f9da1e386f9 to your computer and use it in GitHub Desktop.
Save rust-play/3186ef3d443784bba4839f9da1e386f9 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
use std::time::Instant;
fn main() {
let mut a: Vec<i32> = vec![1; 10000];
let mut b: Vec<i32> = vec![1; 10000];
let t = Instant::now();
let mut nv = Vec::with_capacity(10000);
for i in a.iter() {
nv.push(*i * 2);
}
println!("{:?}", t.elapsed().as_nanos());
let t1 = Instant::now();
b.iter().map(|e| *e * 2).collect::<Vec<i32>>();
println!("{:?}", t1.elapsed().as_nanos());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment