|
24 | 24 | #[cfg(test)] #[phase(syntax, link)] extern crate log;
|
25 | 25 | extern crate serialize;
|
26 | 26 | extern crate libc;
|
| 27 | +#[cfg(target_os = "macos")] |
| 28 | +extern crate sync; |
27 | 29 |
|
28 | 30 | use std::io::BufReader;
|
29 | 31 | use std::num;
|
@@ -159,10 +161,16 @@ pub fn precise_time_ns() -> u64 {
|
159 | 161 |
|
160 | 162 | #[cfg(target_os = "macos")]
|
161 | 163 | fn os_precise_time_ns() -> u64 {
|
162 |
| - let time = unsafe { imp::mach_absolute_time() }; |
163 |
| - let mut info = libc::mach_timebase_info { numer: 0, denom: 0 }; |
164 |
| - unsafe { imp::mach_timebase_info(&mut info); } |
165 |
| - return time * ((info.numer / info.denom) as u64); |
| 164 | + static mut TIMEBASE: libc::mach_timebase_info = libc::mach_timebase_info { numer: 0, |
| 165 | + denom: 0 }; |
| 166 | + static mut ONCE: sync::one::Once = sync::one::ONCE_INIT; |
| 167 | + unsafe { |
| 168 | + ONCE.doit(|| { |
| 169 | + imp::mach_timebase_info(&mut TIMEBASE); |
| 170 | + }); |
| 171 | + let time = imp::mach_absolute_time(); |
| 172 | + time * TIMEBASE.numer as u64 / TIMEBASE.denom as u64 |
| 173 | + } |
166 | 174 | }
|
167 | 175 |
|
168 | 176 | #[cfg(not(windows), not(target_os = "macos"))]
|
@@ -1080,11 +1088,13 @@ pub fn strftime(format: &str, tm: &Tm) -> StrBuf {
|
1080 | 1088 |
|
1081 | 1089 | #[cfg(test)]
|
1082 | 1090 | mod tests {
|
| 1091 | + extern crate test; |
1083 | 1092 | use super::{Timespec, get_time, precise_time_ns, precise_time_s, tzset,
|
1084 | 1093 | at_utc, at, strptime};
|
1085 | 1094 |
|
1086 | 1095 | use std::f64;
|
1087 | 1096 | use std::result::{Err, Ok};
|
| 1097 | + use self::test::Bencher; |
1088 | 1098 |
|
1089 | 1099 | #[cfg(windows)]
|
1090 | 1100 | fn set_time_zone() {
|
@@ -1520,4 +1530,9 @@ mod tests {
|
1520 | 1530 | test_strftime();
|
1521 | 1531 | test_timespec_eq_ord();
|
1522 | 1532 | }
|
| 1533 | + |
| 1534 | + #[bench] |
| 1535 | + fn bench_precise_time_ns(b: &mut Bencher) { |
| 1536 | + b.iter(|| precise_time_ns()) |
| 1537 | + } |
1523 | 1538 | }
|
0 commit comments