Skip to content

Instantly share code, notes, and snippets.

@ljedrz

ljedrz/main.rs Secret

Created July 26, 2018 15:26
Show Gist options
  • Save ljedrz/568e97621b749849684c1da71c27dceb to your computer and use it in GitHub Desktop.
Save ljedrz/568e97621b749849684c1da71c27dceb to your computer and use it in GitHub Desktop.
benchmark a push loop against extend()
#![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