criterion/
format.rs

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
use Throughput;

pub fn change(pct: f64, signed: bool) -> String {
    if signed {
        format!("{:>+6}%", signed_short(pct * 1e2))
    } else {
        format!("{:>6}%", short(pct * 1e2))
    }
}

fn short(n: f64) -> String {
    if n < 10.0 {
        format!("{:.4}", n)
    } else if n < 100.0 {
        format!("{:.3}", n)
    } else if n < 1000.0 {
        format!("{:.2}", n)
    } else if n < 10000.0 {
        format!("{:.1}", n)
    } else {
        format!("{:.0}", n)
    }
}

fn signed_short(n: f64) -> String {
    let n_abs = n.abs();

    if n_abs < 10.0 {
        format!("{:+.4}", n)
    } else if n_abs < 100.0 {
        format!("{:+.3}", n)
    } else if n_abs < 1000.0 {
        format!("{:+.2}", n)
    } else if n_abs < 10000.0 {
        format!("{:+.1}", n)
    } else {
        format!("{:+.0}", n)
    }
}

pub fn time(ns: f64) -> String {
    if ns < 1.0 {
        format!("{:>6} ps", short(ns * 1e3))
    } else if ns < 10f64.powi(3) {
        format!("{:>6} ns", short(ns))
    } else if ns < 10f64.powi(6) {
        format!("{:>6} us", short(ns / 1e3))
    } else if ns < 10f64.powi(9) {
        format!("{:>6} ms", short(ns / 1e6))
    } else {
        format!("{:>6} s", short(ns / 1e9))
    }
}

pub fn throughput(throughput: &Throughput, ns: f64) -> String {
    match *throughput {
        Throughput::Bytes(bytes) => bytes_per_second(f64::from(bytes) * (1e9 / ns)),
        Throughput::Elements(elems) => elements_per_second(f64::from(elems) * (1e9 / ns)),
    }
}

pub fn bytes_per_second(bytes_per_second: f64) -> String {
    if bytes_per_second < 1024.0 {
        format!("{:>6}   B/s", short(bytes_per_second))
    } else if bytes_per_second < 1024.0 * 1024.0 {
        format!("{:>6} KiB/s", short(bytes_per_second / 1024.0))
    } else if bytes_per_second < 1024.0 * 1024.0 * 1024.0 {
        format!("{:>6} MiB/s", short(bytes_per_second / (1024.0 * 1024.0)))
    } else {
        format!(
            "{:>6} GiB/s",
            short(bytes_per_second / (1024.0 * 1024.0 * 1024.0))
        )
    }
}

pub fn elements_per_second(elements_per_second: f64) -> String {
    if elements_per_second < 1000.0 {
        format!("{:>6}  elem/s", short(elements_per_second))
    } else if elements_per_second < 1000.0 * 1000.0 {
        format!("{:>6} Kelem/s", short(elements_per_second / 1000.0))
    } else if elements_per_second < 1000.0 * 1000.0 * 1000.0 {
        format!(
            "{:>6} Melem/s",
            short(elements_per_second / (1000.0 * 1000.0))
        )
    } else {
        format!(
            "{:>6} Gelem/s",
            short(elements_per_second / (1000.0 * 1000.0 * 1000.0))
        )
    }
}

pub fn iter_count(iterations: u64) -> String {
    if iterations < 10_000 {
        format!("{} iterations", iterations)
    } else if iterations < 1_000_000 {
        format!("{:.0}k iterations", (iterations as f64) / 1000.0)
    } else if iterations < 10_000_000 {
        format!("{:.1}M iterations", (iterations as f64) / (1000.0 * 1000.0))
    } else if iterations < 1_000_000_000 {
        format!("{:.0}M iterations", (iterations as f64) / (1000.0 * 1000.0))
    } else if iterations < 10_000_000_000 {
        format!(
            "{:.1}B iterations",
            (iterations as f64) / (1000.0 * 1000.0 * 1000.0)
        )
    } else {
        format!(
            "{:.0}B iterations",
            (iterations as f64) / (1000.0 * 1000.0 * 1000.0)
        )
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn short_max_len() {
        let mut float = 1.0;
        while float < 999999.9 {
            let string = short(float);
            println!("{}", string);
            assert!(string.len() <= 6);
            float *= 2.0;
        }
    }

    #[test]
    fn signed_short_max_len() {
        let mut float = -1.0;
        while float > -999999.9 {
            let string = signed_short(float);
            println!("{}", string);
            assert!(string.len() <= 7);
            float *= 2.0;
        }
    }
}