Skip to content

Instantly share code, notes, and snippets.

Created April 3, 2015 00:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/b66c4daa6296996235eb to your computer and use it in GitHub Desktop.
Save anonymous/b66c4daa6296996235eb to your computer and use it in GitHub Desktop.
#![feature(test)]
extern crate test;
use std::io::Read;
#[bench]
fn read_to_end_1024(b: &mut test::Bencher) {
let buf = [1u8; 1024];
let mut src = &buf[..];
b.iter(|| {
let mut dst = Vec::with_capacity(1024);
src.read_to_end(&mut dst)
});
}
#[bench]
fn read_to_end_64k(b: &mut test::Bencher) {
let buf = [1u8; 1024 * 64];
let mut src = &buf[..];
b.iter(|| {
let mut dst = Vec::with_capacity(64 * 1024);
src.read_to_end(&mut dst)
});
}
#[bench]
fn read_to_end_normal_test(b: &mut test::Bencher) {
let buf = [1u8; 100000];
let mut src = &buf[..];
b.iter(|| {
let mut dst = Vec::new();
src.read_to_end(&mut dst)
});
}
#[bench]
fn read_to_end_small_test(b: &mut test::Bencher) {
let buf = [1u8; 1000];
let mut src = &buf[..];
b.iter(|| {
let mut dst = Vec::new();
src.read_to_end(&mut dst)
});
}
#[test]
fn read_to_end_doesnt_expand_capacity_unless_necessary() {
const BUF_SIZE: usize = 1024;
let buf = [1u8; BUF_SIZE];
let mut src = &buf[..];
let mut dst = Vec::with_capacity(BUF_SIZE);
let init_capacity = dst.capacity();
assert_eq!(src.read_to_end(&mut dst).unwrap(), BUF_SIZE);
assert_eq!(init_capacity, dst.capacity());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment