Skip to content

Commit

Permalink
tweak warm_up function to get a better estimate of the measuring time
Browse files Browse the repository at this point in the history
the previous version didn't consider the time used by the setup function.
  • Loading branch information
Jorge Aparicio committed Feb 9, 2016
1 parent 8338eca commit 47e65f9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
12 changes: 7 additions & 5 deletions src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::io::BufReader;
use std::process::{Child, ChildStderr, ChildStdin, ChildStdout, Command, Stdio};
use std::time::{Duration, Instant};

use DurationExt;
use routine::Routine;

// A two-way channel to the standard streams of a child process
Expand Down Expand Up @@ -91,14 +92,15 @@ impl Routine for Program {
fn warm_up(&mut self, how_long_ns: Duration) -> (u64, u64) {
let mut iters = 1;

let mut total_iters = 0;
let start = Instant::now();
loop {
let elapsed =
self.send(iters).recv().trim().parse().ok().
expect("Couldn't parse the program output");
self.send(iters).recv();

if start.elapsed() > how_long_ns {
return (elapsed, iters);
total_iters += iters;
let elapsed = start.elapsed();
if elapsed > how_long_ns {
return (elapsed.to_nanos(), total_iters);
}

iters *= 2;
Expand Down
7 changes: 5 additions & 2 deletions src/routine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,15 @@ impl<F> Routine for Function<F> where F: FnMut(&mut Bencher) {
let Function(ref mut f) = *self;
let mut b = Bencher { iters: 1, elapsed: Duration::from_secs(0) };

let mut total_iters = 0;
let start = Instant::now();
loop {
(*f)(&mut b);

if start.elapsed() > how_long {
return (b.elapsed.to_nanos(), b.iters);
total_iters += b.iters;
let elapsed = start.elapsed();
if elapsed > how_long {
return (elapsed.to_nanos(), total_iters);
}

b.iters *= 2;
Expand Down

0 comments on commit 47e65f9

Please sign in to comment.