auto merge of #7980 : graydon/rust/misc-benchmarks, r=catamorphism

Some machinery for enabling #[bench] benchmarks in std and some examples showing how to write them.
This commit is contained in:
bors 2013-07-23 22:46:39 -07:00
commit 7f96eb58d2
9 changed files with 238 additions and 2 deletions

View file

@ -703,3 +703,27 @@ mod test {
assert_eq!(n, None);
}
}
#[cfg(test)]
mod bench {
use extra::test::BenchHarness;
use rand::{XorShiftRng,RngUtil};
use uint;
use float;
#[bench]
fn uint_to_str_rand(bh: &mut BenchHarness) {
let mut rng = XorShiftRng::new();
do bh.iter {
uint::to_str(rng.gen());
}
}
#[bench]
fn float_to_str_rand(bh: &mut BenchHarness) {
let mut rng = XorShiftRng::new();
do bh.iter {
float::to_str(rng.gen());
}
}
}

View file

@ -81,3 +81,28 @@ pub trait Shr<RHS,Result> {
pub trait Index<Index,Result> {
fn index(&self, index: &Index) -> Result;
}
#[cfg(test)]
mod bench {
use extra::test::BenchHarness;
use ops::Drop;
// Overhead of dtors
struct HasDtor {
x: int
}
impl Drop for HasDtor {
fn drop(&self) {
}
}
#[bench]
fn alloc_obj_with_dtor(bh: &mut BenchHarness) {
do bh.iter {
HasDtor { x : 10 };
}
}
}

View file

@ -890,7 +890,7 @@ pub fn random<T: Rand>() -> T {
}
#[cfg(test)]
mod tests {
mod test {
use option::{Option, Some};
use super::*;
@ -1109,3 +1109,37 @@ mod tests {
}
}
}
#[cfg(test)]
mod bench {
use extra::test::BenchHarness;
use rand::*;
use sys::size_of;
#[bench]
fn rand_xorshift(bh: &mut BenchHarness) {
let mut rng = XorShiftRng::new();
do bh.iter {
rng.gen::<uint>();
}
bh.bytes = size_of::<uint>() as u64;
}
#[bench]
fn rand_isaac(bh: &mut BenchHarness) {
let mut rng = IsaacRng::new();
do bh.iter {
rng.gen::<uint>();
}
bh.bytes = size_of::<uint>() as u64;
}
#[bench]
fn rand_shuffle_100(bh: &mut BenchHarness) {
let mut rng = XorShiftRng::new();
let x : &mut[uint] = [1,..100];
do bh.iter {
rng.shuffle_mut(x);
}
}
}

View file

@ -101,3 +101,22 @@ pub unsafe fn exchange_free_(ptr: *c_char) {
pub unsafe fn exchange_free(ptr: *c_char) {
free(ptr as *c_void);
}
#[cfg(test)]
mod bench {
use extra::test::BenchHarness;
#[bench]
fn alloc_owned_small(bh: &mut BenchHarness) {
do bh.iter {
~10;
}
}
#[bench]
fn alloc_owned_big(bh: &mut BenchHarness) {
do bh.iter {
~[10, ..1000];
}
}
}

View file

@ -135,3 +135,22 @@ extern {
fn rust_boxed_region_free(region: *BoxedRegion, box: *OpaqueBox);
fn rust_current_boxed_region() -> *BoxedRegion;
}
#[cfg(test)]
mod bench {
use extra::test::BenchHarness;
#[bench]
fn alloc_managed_small(bh: &mut BenchHarness) {
do bh.iter {
@10;
}
}
#[bench]
fn alloc_managed_big(bh: &mut BenchHarness) {
do bh.iter {
@[10, ..1000];
}
}
}

View file

@ -63,6 +63,9 @@ they contained the following prologue:
#[deny(non_camel_case_types)];
#[deny(missing_doc)];
// Make extra accessible for benchmarking
#[cfg(test)] extern mod extra(vers="0.8-pre");
// Make std testable by not duplicating lang items. See #2912
#[cfg(test)] extern mod realstd(name = "std");
#[cfg(test)] pub use kinds = realstd::kinds;

View file

@ -3532,3 +3532,50 @@ mod tests {
assert_eq!(5, sum_len([s.as_slice()]));
}
}
#[cfg(test)]
mod bench {
use extra::test::BenchHarness;
use str;
#[bench]
fn is_utf8_100_ascii(bh: &mut BenchHarness) {
let s = bytes!("Hello there, the quick brown fox jumped over the lazy dog! \
Lorem ipsum dolor sit amet, consectetur. ");
assert_eq!(100, s.len());
do bh.iter {
str::is_utf8(s);
}
}
#[bench]
fn is_utf8_100_multibyte(bh: &mut BenchHarness) {
let s = bytes!("𐌀𐌖𐌋𐌄𐌑𐌉ปรدولة الكويتทศไทย中华𐍅𐌿𐌻𐍆𐌹𐌻𐌰");
assert_eq!(100, s.len());
do bh.iter {
str::is_utf8(s);
}
}
#[bench]
fn map_chars_100_ascii(bh: &mut BenchHarness) {
let s = "HelloHelloHelloHelloHelloHelloHelloHelloHelloHello\
HelloHelloHelloHelloHelloHelloHelloHelloHelloHello";
do bh.iter {
s.map_chars(|c| ((c as uint) + 1) as char);
}
}
#[bench]
fn map_chars_100_multibytes(bh: &mut BenchHarness) {
let s = "𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑\
𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑\
𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑\
𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑";
do bh.iter {
s.map_chars(|c| ((c as uint) + 1) as char);
}
}
}

View file

@ -195,3 +195,68 @@ mod tests {
unsafe { assert_eq!(did_run, true); }
}
}
/// Completely miscellaneous language-construct benchmarks.
#[cfg(test)]
mod bench {
use extra::test::BenchHarness;
use option::{Some,None};
// Static/dynamic method dispatch
struct Struct {
field: int
}
trait Trait {
fn method(&self) -> int;
}
impl Trait for Struct {
fn method(&self) -> int {
self.field
}
}
#[bench]
fn trait_vtable_method_call(bh: &mut BenchHarness) {
let s = Struct { field: 10 };
let t = &s as &Trait;
do bh.iter {
t.method();
}
}
#[bench]
fn trait_static_method_call(bh: &mut BenchHarness) {
let s = Struct { field: 10 };
do bh.iter {
s.method();
}
}
// Overhead of various match forms
#[bench]
fn match_option_some(bh: &mut BenchHarness) {
let x = Some(10);
do bh.iter {
let _q = match x {
Some(y) => y,
None => 11
};
}
}
#[bench]
fn match_vec_pattern(bh: &mut BenchHarness) {
let x = [1,2,3,4,5,6];
do bh.iter {
let _q = match x {
[1,2,3,.._] => 10,
_ => 11
};
}
}
}

View file

@ -68,7 +68,7 @@ public:
return &reinterpret_cast<const cratemap_v0 *>(this)->
m_children[0];
case 1:
return &m_children[1];
return &m_children[0];
default: assert(false && "Unknown crate map version!");
return NULL; // Appease -Werror=return-type
}