projects
/
Mitarbeiter
/
Tim-Zeitz
/
stud-rust-base.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
9d00f05
)
remove time crate and use std lib
author
Tim "tim3z" Zeitz
<dev@tim3z.net>
Thu, 16 Dec 2021 09:46:05 +0000
(10:46 +0100)
committer
Tim "tim3z" Zeitz
<dev@tim3z.net>
Thu, 16 Dec 2021 09:46:05 +0000
(10:46 +0100)
src/time.rs
patch
|
blob
|
history
diff --git
a/src/time.rs
b/src/time.rs
index 687f3e424a86d090431c335b5cfb196545bb5476..33c720c83b3ec2a8eef407d87e94f08f9b7b8d39 100644
(file)
--- a/
src/time.rs
+++ b/
src/time.rs
@@
-1,28
+1,29
@@
//! This module contains a few utilities to measure how long executing algorithms takes.
//! This module contains a few utilities to measure how long executing algorithms takes.
-//! It utilizes the `time` crate.
+
+use std::time::*;
/// This function will measure how long it takes to execute the given lambda,
/// print the time and return the result of the lambda.
pub fn report_time<Out, F: FnOnce() -> Out>(name: &str, f: F) -> Out {
/// This function will measure how long it takes to execute the given lambda,
/// print the time and return the result of the lambda.
pub fn report_time<Out, F: FnOnce() -> Out>(name: &str, f: F) -> Out {
- let start =
time
::now();
+ let start =
Instant
::now();
eprintln!("starting {}", name);
let res = f();
eprintln!("starting {}", name);
let res = f();
- eprintln!("done {} - took: {}
", name, (time::now() - start
));
+ eprintln!("done {} - took: {}
s", name, start.elapsed().as_secs_f64(
));
res
}
/// This function will measure how long it takes to execute the given lambda
/// and return a tuple of the result of the lambda and a duration object.
res
}
/// This function will measure how long it takes to execute the given lambda
/// and return a tuple of the result of the lambda and a duration object.
-pub fn measure<Out, F: FnOnce() -> Out>(f: F) -> (Out,
time::
Duration) {
- let start =
time
::now();
+pub fn measure<Out, F: FnOnce() -> Out>(f: F) -> (Out, Duration) {
+ let start =
Instant
::now();
let res = f();
let res = f();
- (res,
time::now() - start
)
+ (res,
start.elapsed()
)
}
/// A struct to repeatedly measure the time passed since the timer was started
#[derive(Debug)]
pub struct Timer {
}
/// A struct to repeatedly measure the time passed since the timer was started
#[derive(Debug)]
pub struct Timer {
- start:
time::Tm
+ start:
Instant,
}
impl Default for Timer {
}
impl Default for Timer {
@@
-34,21
+35,26
@@
impl Default for Timer {
impl Timer {
/// Create and start a new `Timer`
pub fn new() -> Timer {
impl Timer {
/// Create and start a new `Timer`
pub fn new() -> Timer {
- Timer { start:
time
::now() }
+ Timer { start:
Instant
::now() }
}
/// Reset the `Timer`
pub fn restart(&mut self) {
}
/// Reset the `Timer`
pub fn restart(&mut self) {
- self.start =
time
::now();
+ self.start =
Instant
::now();
}
/// Print the passed time in ms since the timer was started
pub fn report_passed_ms(&self) {
}
/// Print the passed time in ms since the timer was started
pub fn report_passed_ms(&self) {
- eprintln!("{}ms",
(time::now() - self.start).num_milliseconds()
);
+ eprintln!("{}ms",
self.start.elapsed().as_secs_f64() * 1000.0
);
}
/// Return the number of ms passed since the timer was started as a `i64`
}
/// Return the number of ms passed since the timer was started as a `i64`
- pub fn get_passed_ms(&self) -> i64 {
- (time::now() - self.start).num_milliseconds()
+ pub fn get_passed_ms(&self) -> f64 {
+ self.start.elapsed().as_secs_f64() * 1000.0
+ }
+
+ /// Return the number of ms passed since the timer was started as a `std::time::Duration`
+ pub fn get_passed(&self) -> Duration {
+ self.start.elapsed()
}
}
}
}