Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace time with std::time #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,3 @@ license = "MIT"
[lib]
name = "timeit"
doctest = false

[dependencies]
time = "0.1.26"
20 changes: 5 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,21 @@
//! }
//! });
//! ```
extern crate time;

use time::Timespec;

/// A shortcut to time's `get_time` function. This is so that the user of timeit doesn't have to
/// separately add a dependency for the time crate.
pub fn get_time() -> Timespec {
use time::get_time;
get_time()
}

#[macro_export]
/// Runs a block a specified number of times and returns the average time of execution.
macro_rules! timeit_loops {
($loops:expr, $code:block) => ({
use timeit::get_time;
use std::time::Instant;

let n = $loops;
let start = get_time();
let start = Instant::now();
for _ in 0..n {
$code
}
let end = get_time();
let sec = (end.sec - start.sec) as f64 +
(end.nsec - start.nsec) as f64 / 1_000_000_000.0;
let elapsed = start.elapsed();
let sec = elapsed.as_secs() as f64 +
elapsed.subsec_nanos() as f64 / 1_000_000_000.0;

sec / (n as f64)
})
Expand Down