11use std:: env;
22use std:: fmt;
33use std:: mem;
4- use time;
4+ use std :: time;
55use std:: iter:: repeat;
66use std:: cell:: RefCell ;
77
8- thread_local ! ( static PROFILE_STACK : RefCell <Vec <u64 >> = RefCell :: new( Vec :: new( ) ) ) ;
8+ thread_local ! ( static PROFILE_STACK : RefCell <Vec <time :: Instant >> = RefCell :: new( Vec :: new( ) ) ) ;
99thread_local ! ( static MESSAGES : RefCell <Vec <Message >> = RefCell :: new( Vec :: new( ) ) ) ;
1010
1111type Message = ( usize , u64 , String ) ;
@@ -21,7 +21,7 @@ fn enabled_level() -> Option<usize> {
2121pub fn start < T : fmt:: Display > ( desc : T ) -> Profiler {
2222 if enabled_level ( ) . is_none ( ) { return Profiler { desc : String :: new ( ) } }
2323
24- PROFILE_STACK . with ( |stack| stack. borrow_mut ( ) . push ( time:: precise_time_ns ( ) ) ) ;
24+ PROFILE_STACK . with ( |stack| stack. borrow_mut ( ) . push ( time:: Instant :: now ( ) ) ) ;
2525
2626 Profiler {
2727 desc : desc. to_string ( ) ,
@@ -36,7 +36,8 @@ impl Drop for Profiler {
3636 } ;
3737
3838 let start = PROFILE_STACK . with ( |stack| stack. borrow_mut ( ) . pop ( ) . unwrap ( ) ) ;
39- let end = time:: precise_time_ns ( ) ;
39+ let duration = start. elapsed ( ) ;
40+ let duration_ms = duration. as_secs ( ) * 1000 + ( duration. subsec_nanos ( ) / 1000000 ) as u64 ;
4041
4142 let stack_len = PROFILE_STACK . with ( |stack| stack. borrow ( ) . len ( ) ) ;
4243 if stack_len == 0 {
@@ -47,7 +48,7 @@ impl Drop for Profiler {
4748 if l != lvl { continue }
4849 println ! ( "{} {:6}ms - {}" ,
4950 repeat( " " ) . take( lvl + 1 ) . collect:: <String >( ) ,
50- time / 1000000 , msg) ;
51+ time, msg) ;
5152
5253 print ( lvl + 1 , & msgs[ last..i] , enabled) ;
5354 last = i;
@@ -56,14 +57,14 @@ impl Drop for Profiler {
5657 }
5758 MESSAGES . with ( |msgs_rc| {
5859 let mut msgs = msgs_rc. borrow_mut ( ) ;
59- msgs. push ( ( 0 , end - start ,
60+ msgs. push ( ( 0 , duration_ms ,
6061 mem:: replace ( & mut self . desc , String :: new ( ) ) ) ) ;
6162 print ( 0 , & msgs, enabled) ;
6263 } ) ;
6364 } else {
6465 MESSAGES . with ( |msgs| {
6566 let msg = mem:: replace ( & mut self . desc , String :: new ( ) ) ;
66- msgs. borrow_mut ( ) . push ( ( stack_len, end - start , msg) ) ;
67+ msgs. borrow_mut ( ) . push ( ( stack_len, duration_ms , msg) ) ;
6768 } ) ;
6869 }
6970 }
0 commit comments