extra::test: add an opaque function to assist with accurate

benchmarking.

This allows a result to be marked as "used" by passing it to a function
LLVM cannot see inside. By making `iter` generic and using this
`black_box` on the result benchmarks can get this behaviour simply by
returning their computation.
This commit is contained in:
Huon Wilson 2014-02-08 17:59:23 +11:00
parent b60bed9791
commit b029a18820
2 changed files with 15 additions and 3 deletions

View File

@ -29,7 +29,7 @@ Rust extras are part of the standard Rust distribution.
html_favicon_url = "http://www.rust-lang.org/favicon.ico",
html_root_url = "http://static.rust-lang.org/doc/master")];
#[feature(macro_rules, globs, managed_boxes)];
#[feature(macro_rules, globs, managed_boxes, asm)];
#[deny(non_camel_case_types)];
#[deny(missing_doc)];

View File

@ -1091,13 +1091,25 @@ impl MetricMap {
// Benchmarking
/// A function that is opaque to the optimiser, to allow benchmarks to
/// pretend to use outputs to assist in avoiding dead-code
/// elimination.
///
/// This function is a no-op, and does not even read from `dummy`.
pub fn black_box<T>(dummy: T) {
// we need to "use" the argument in some way LLVM can't
// introspect.
unsafe {asm!("" : : "r"(&dummy))}
}
impl BenchHarness {
/// Callback for benchmark functions to run in their body.
pub fn iter(&mut self, inner: ||) {
pub fn iter<T>(&mut self, inner: || -> T) {
self.ns_start = precise_time_ns();
let k = self.iterations;
for _ in range(0u64, k) {
inner();
black_box(inner());
}
self.ns_end = precise_time_ns();
}