Skip to content

Commit c587418

Browse files
authored
Functions for more precise time measurement (#77)
1 parent 995c39c commit c587418

File tree

5 files changed

+46
-1
lines changed

5 files changed

+46
-1
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ dmsort = {version = "1.0.0", optional = true}
4545
toml-dep = { version = "0.5.8", package="toml", optional = true }
4646

4747
[features]
48-
default = ["cellularnoise", "dmi", "file", "git", "http", "json", "log", "noise", "sql", "toml", "url"]
48+
default = ["cellularnoise", "dmi", "file", "git", "http", "json", "log", "noise", "sql", "time", "toml", "url"]
4949

5050
# default features
5151
cellularnoise = ["rand"]
@@ -56,6 +56,7 @@ http = ["reqwest", "serde", "serde_json", "once_cell", "jobs"]
5656
json = ["serde", "serde_json"]
5757
log = ["chrono"]
5858
sql = ["mysql", "serde", "serde_json", "once_cell", "dashmap", "jobs"]
59+
time = []
5960
toml = ["serde", "serde_json", "toml-dep"]
6061
url = ["url-dep", "percent-encoding"]
6162

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ The default features are:
8989
* sql: Asynchronous MySQL/MariaDB client library.
9090
* noise: 2d Perlin noise.
9191
* toml: TOML parser.
92+
* time: High-accuracy time measuring.
9293

9394
Additional features are:
9495
* hash: Faster replacement for `md5`, support for SHA-1, SHA-256, and SHA-512. Requires OpenSSL on Linux.

dmsrc/time.dm

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#define rustg_time_microseconds(id) text2num(call(RUST_G, "time_microseconds")(id))
2+
#define rustg_time_milliseconds(id) text2num(call(RUST_G, "time_milliseconds")(id))
3+
#define rustg_time_reset(id) call(RUST_G, "time_reset")(id)

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ pub mod log;
2626
pub mod noise_gen;
2727
#[cfg(feature = "sql")]
2828
pub mod sql;
29+
#[cfg(feature = "time")]
30+
pub mod time;
2931
#[cfg(feature = "toml")]
3032
pub mod toml;
3133
#[cfg(feature = "unzip")]

src/time.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use std::{
2+
cell::RefCell,
3+
collections::hash_map::{Entry, HashMap},
4+
time::Instant,
5+
};
6+
7+
thread_local!( static INSTANTS: RefCell<HashMap<String, Instant>> = RefCell::new(HashMap::new()) );
8+
9+
byond_fn! { time_microseconds(instant_id) {
10+
INSTANTS.with(|instants| {
11+
let mut map = instants.borrow_mut();
12+
let instant = match map.entry(instant_id.into()) {
13+
Entry::Occupied(elem) => elem.into_mut(),
14+
Entry::Vacant(elem) => elem.insert(Instant::now()),
15+
};
16+
Some(instant.elapsed().as_micros().to_string())
17+
})
18+
} }
19+
20+
byond_fn! { time_milliseconds(instant_id) {
21+
INSTANTS.with(|instants| {
22+
let mut map = instants.borrow_mut();
23+
let instant = match map.entry(instant_id.into()) {
24+
Entry::Occupied(elem) => elem.into_mut(),
25+
Entry::Vacant(elem) => elem.insert(Instant::now()),
26+
};
27+
Some(instant.elapsed().as_millis().to_string())
28+
})
29+
} }
30+
31+
byond_fn! { time_reset(instant_id) {
32+
INSTANTS.with(|instants| {
33+
let mut map = instants.borrow_mut();
34+
map.insert(instant_id.into(), Instant::now());
35+
Some("")
36+
})
37+
} }
38+

0 commit comments

Comments
 (0)