Skip to content

Commit

Permalink
std: various additional language benchmarks in util.
Browse files Browse the repository at this point in the history
  • Loading branch information
graydon committed Jul 22, 2013
1 parent ca5ed4c commit d9c0634
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions src/libstd/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
}
}
}

0 comments on commit d9c0634

Please sign in to comment.