-
-
Save ljedrz/568e97621b749849684c1da71c27dceb to your computer and use it in GitHub Desktop.
benchmark a push loop against extend()
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
#![feature(test)] | |
extern crate test; | |
use test::Bencher; | |
fn push_loop<T: Iterator<Item = usize>>(iter: T) -> Vec<usize> { | |
let mut vec = vec![]; | |
for e in iter { | |
vec.push(e); | |
} | |
vec | |
} | |
fn extension<T: Iterator<Item = usize>>(iter: T) -> Vec<usize> { | |
let mut vec = vec![]; | |
vec.extend(iter); | |
vec | |
} | |
#[bench] | |
fn bench_push_loop(b: &mut Bencher) { | |
let n = test::black_box(100); | |
b.iter(|| push_loop(0..n) ) | |
} | |
#[bench] | |
fn bench_extension(b: &mut Bencher) { | |
let n = test::black_box(100); | |
b.iter(|| extension(0..n) ) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment