Created
April 3, 2015 00:54
-
-
Save anonymous/b66c4daa6296996235eb to your computer and use it in GitHub Desktop.
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 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