1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*!
## Notice: **Have to use release mod(with optimizations)**

## example: 

### on cargo.toml:

```toml
[dependencies]
stderr = "0.8.0"
bench_timer = "0.1.0"
```

or

```toml
[dependencies]
stderr = "0.8.0"
bench_timer = { git = "https://github.com/biluohc/bench_timer", branch = "master", version = "0.1.0"}
```

### on code: 

```rust
#[macro_use]
extern crate stderr;
#[macro_use]
extern crate bench_timer;

fn main() {
    let msg = std::env::args()
        .skip(1)
        .next()
        .unwrap_or("fuck u".to_owned());

    timer_sort!(3,
                100000,
                to_owed(),
                new(),
                default(),
                format(),
                insert_e(&msg),
                push_str(&msg),
                format_s(&msg),
                insert_0(&msg),
                mul_args_add(&msg, ": cargo"),
                mul_args_like_push_str(&msg, ": cargo")
                );
}

#[inline(always)]
fn to_owed() {
    let s = "".to_owned();
    dbln!("{}", s);
}
#[inline(always)]
fn new() {
    let s = String::new();
    dbln!("{}", s);
}
#[inline(always)]
fn default() {
    let s = String::default();
    dbln!("{}", s);
}
#[inline(always)]
fn format() {
    let s = format!("{}", "");
    dbln!("{}", s);
}
#[inline(always)]
fn push_str(msg: &str) {
    let mut s = msg.to_owned();
    s.push_str(": cargo");
    dbln!("{}", s);
}

#[inline(always)]
fn format_s(msg: &str) {
    let s = format!("{}: cargo", msg);
    dbln!("{}", s);
}
#[inline(always)]
fn insert_0(msg: &str) {
    let mut s = ": cargo".to_owned();
    s.insert_str(0, msg);
    dbln!("{}", s);
}
#[inline(always)]
fn insert_e(msg: &str) {
    let mut s = ": cargo".to_owned();
    let len = s.len();
    s.insert_str(len, msg);
    dbln!("{}", s);
}
#[inline(always)]
fn mul_args_add(msg0: &str, msg1: &str) {
    let s = msg0.to_owned() + msg1;
    dbln!("{}", s);
}
#[inline(always)]
fn mul_args_like_push_str(msg0: &str, msg1: &str) {
    let mut s = msg0.to_owned();
    s.push_str(msg1);
    dbln!("{}", s);
}
```
*/
use std::time::{SystemTime, SystemTimeError, Duration};

pub fn timer_avg<F>(mut f: F, times: u32) -> Result<Duration, SystemTimeError>
    where F: FnMut()
{
    let st = SystemTime::now();
    for _ in 0..times {
        f();
    }
    SystemTime::now().duration_since(st).map(|t| t / times)
}

#[macro_export]
macro_rules! timer_times {
     ($times_macro:expr, $times_fn:expr, $($fn_name: ident($($args: expr),*)),+) => {
         for _ in 0..$times_macro {
             $(println!("{}: {:?}",stringify!($fn_name), $crate::timer_avg(||$fn_name($($args),*), $times_fn).unwrap()); )+
             println!();
         }
     }
}

#[macro_export]
macro_rules! timer_sort {
    // tt 貌似也可以,只是也不能支持通过类型调用函数。
     ($times_macro:expr, $times_fn:expr, $($fn_name: tt($($args: expr),*)),+) => {
         use std::time::Duration;
         for _ in 0..$times_macro {
             let mut vs:Vec<(&str, Duration)> =Vec::default();
             $(vs.push((stringify!($fn_name),$crate::timer_avg(||$fn_name($($args),*), $times_fn).unwrap()));)+   
            vs.sort_by(|&(_,ref a),&(_,ref b)| b.cmp(a));
            let max_len= vs.as_slice().iter() .fold(0,| acc, &(s,_) | if s.len() > acc { s.len() } else { acc });            
            let blanks_fix= |msg: &str| {
                let mut tmp =String::default();
                for _ in 0..max_len- msg.len()
                 {tmp.push(' ');}
                tmp
            };
            vs.iter().map(|&(fn_name,t)|
            println!("{}{}: {:?}",fn_name,blanks_fix(fn_name),t)
             ).count();
             println!();
         }
     }
}