divan 0.1.18

Statistically-comfy benchmarking library.
Documentation
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
use std::{fmt, ops, time::Duration};

use crate::util;

/// [Picosecond](https://en.wikipedia.org/wiki/Picosecond)-precise [`Duration`].
#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord)]
#[repr(transparent)]
pub(crate) struct FineDuration {
    pub picos: u128,
}

impl From<Duration> for FineDuration {
    #[inline]
    fn from(duration: Duration) -> Self {
        Self {
            picos: duration
                .as_nanos()
                .checked_mul(1_000)
                .unwrap_or_else(|| panic!("{duration:?} is too large to fit in `FineDuration`")),
        }
    }
}

impl fmt::Display for FineDuration {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let sig_figs = f.precision().unwrap_or(4);

        let picos = self.picos;
        let mut scale = TimeScale::from_picos(picos);

        // Prefer formatting picoseconds as nanoseconds if we can. This makes
        // picoseconds easier to read because they are almost always alongside
        // nanosecond-scale values.
        if scale == TimeScale::PicoSec && sig_figs > 3 {
            scale = TimeScale::NanoSec;
        }

        let multiple: u128 = {
            let sig_figs = u32::try_from(sig_figs).unwrap_or(u32::MAX);
            10_u128.saturating_pow(sig_figs)
        };

        // TODO: Format without heap allocation.
        let mut str: String = match picos::DAY.checked_mul(multiple) {
            Some(int_day) if picos >= int_day => {
                // Format using integer representation to not lose precision.
                (picos / picos::DAY).to_string()
            }
            _ => {
                // Format using floating point representation.

                // Multiply to allow `sig_figs` digits of fractional precision.
                let val = (((picos * multiple) / scale.picos()) as f64) / multiple as f64;

                util::fmt::format_f64(val, sig_figs)
            }
        };

        str.push(' ');
        str.push_str(scale.suffix());

        // Fill up to specified width.
        if let Some(fill_len) = f.width().and_then(|width| width.checked_sub(str.len())) {
            match f.align() {
                None | Some(fmt::Alignment::Left) => {
                    str.extend(std::iter::repeat(f.fill()).take(fill_len));
                }
                _ => return Err(fmt::Error),
            }
        }

        f.write_str(&str)
    }
}

impl fmt::Debug for FineDuration {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Display::fmt(self, f)
    }
}

impl ops::Add for FineDuration {
    type Output = Self;

    #[inline]
    fn add(self, other: Self) -> Self {
        Self { picos: self.picos + other.picos }
    }
}

impl ops::AddAssign for FineDuration {
    #[inline]
    fn add_assign(&mut self, other: Self) {
        self.picos += other.picos
    }
}

impl<I: Into<u128>> ops::Div<I> for FineDuration {
    type Output = Self;

    #[inline]
    fn div(self, count: I) -> Self {
        Self { picos: self.picos / count.into() }
    }
}

impl FineDuration {
    pub const ZERO: Self = Self { picos: 0 };

    pub const MAX: Self = Self { picos: u128::MAX };

    #[inline]
    pub fn is_zero(&self) -> bool {
        self.picos == 0
    }

    /// Round up to `other` if `self` is zero.
    #[inline]
    pub fn clamp_to(self, other: Self) -> Self {
        if self.is_zero() {
            other
        } else {
            self
        }
    }

    /// Returns the smaller non-zero value.
    #[inline]
    pub fn clamp_to_min(self, other: Self) -> Self {
        if self.is_zero() {
            other
        } else if other.is_zero() {
            self
        } else {
            self.min(other)
        }
    }
}

mod picos {
    pub const NANOS: u128 = 1_000;
    pub const MICROS: u128 = 1_000 * NANOS;
    pub const MILLIS: u128 = 1_000 * MICROS;
    pub const SEC: u128 = 1_000 * MILLIS;
    pub const MIN: u128 = 60 * SEC;
    pub const HOUR: u128 = 60 * MIN;
    pub const DAY: u128 = 24 * HOUR;
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
enum TimeScale {
    PicoSec,
    NanoSec,
    MicroSec,
    MilliSec,
    Sec,
    Min,
    Hour,
    Day,
}

impl TimeScale {
    #[cfg(test)]
    const ALL: &'static [Self] = &[
        Self::PicoSec,
        Self::NanoSec,
        Self::MicroSec,
        Self::MilliSec,
        Self::Sec,
        Self::Min,
        Self::Hour,
        Self::Day,
    ];

    /// Determines the scale of time for representing a number of picoseconds.
    fn from_picos(picos: u128) -> Self {
        use picos::*;

        if picos < NANOS {
            Self::PicoSec
        } else if picos < MICROS {
            Self::NanoSec
        } else if picos < MILLIS {
            Self::MicroSec
        } else if picos < SEC {
            Self::MilliSec
        } else if picos < MIN {
            Self::Sec
        } else if picos < HOUR {
            Self::Min
        } else if picos < DAY {
            Self::Hour
        } else {
            Self::Day
        }
    }

    /// Returns the number of picoseconds needed to reach this scale.
    fn picos(self) -> u128 {
        use picos::*;

        match self {
            Self::PicoSec => 1,
            Self::NanoSec => NANOS,
            Self::MicroSec => MICROS,
            Self::MilliSec => MILLIS,
            Self::Sec => SEC,
            Self::Min => MIN,
            Self::Hour => HOUR,
            Self::Day => DAY,
        }
    }

    /// Returns the unit suffix.
    fn suffix(self) -> &'static str {
        match self {
            Self::PicoSec => "ps",
            Self::NanoSec => "ns",
            Self::MicroSec => "µs",
            Self::MilliSec => "ms",
            Self::Sec => "s",
            Self::Min => "m",
            Self::Hour => "h",
            Self::Day => "d",
        }
    }
}

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

    #[test]
    fn clamp_to() {
        #[track_caller]
        fn test(a: u128, b: u128, expected: u128) {
            assert_eq!(
                FineDuration { picos: a }.clamp_to(FineDuration { picos: b }),
                FineDuration { picos: expected }
            );
        }

        test(0, 0, 0);
        test(0, 1, 1);
        test(0, 2, 2);
        test(0, 3, 3);

        test(1, 0, 1);
        test(1, 1, 1);
        test(1, 2, 1);
        test(1, 3, 1);

        test(2, 0, 2);
        test(2, 1, 2);
        test(2, 2, 2);
        test(2, 3, 2);

        test(3, 0, 3);
        test(3, 1, 3);
        test(3, 2, 3);
        test(3, 3, 3);
    }

    #[test]
    fn clamp_to_min() {
        #[track_caller]
        fn test(a: u128, b: u128, expected: u128) {
            assert_eq!(
                FineDuration { picos: a }.clamp_to_min(FineDuration { picos: b }),
                FineDuration { picos: expected }
            );
        }

        test(0, 0, 0);
        test(0, 1, 1);
        test(0, 2, 2);
        test(0, 3, 3);

        test(1, 0, 1);
        test(1, 1, 1);
        test(1, 2, 1);
        test(1, 3, 1);

        test(2, 0, 2);
        test(2, 1, 1);
        test(2, 2, 2);
        test(2, 3, 2);

        test(3, 0, 3);
        test(3, 1, 1);
        test(3, 2, 2);
        test(3, 3, 3);
    }

    #[allow(clippy::zero_prefixed_literal)]
    mod fmt {
        use super::*;

        #[track_caller]
        fn test(picos: u128, expected: &str) {
            let duration = FineDuration { picos };
            assert_eq!(duration.to_string(), expected);
            assert_eq!(format!("{duration:.4}"), expected);
            assert_eq!(format!("{duration:<0}"), expected);
        }

        macro_rules! assert_fmt_eq {
            ($input:literal, $expected:literal) => {
                assert_eq!(format!($input), format!($expected));
            };
        }

        #[test]
        fn precision() {
            for &scale in TimeScale::ALL {
                let base_duration = FineDuration { picos: scale.picos() };
                let incr_duration = FineDuration { picos: scale.picos() + 1 };

                if scale == TimeScale::PicoSec {
                    assert_eq!(format!("{base_duration:.0}"), "1 ps");
                    assert_eq!(format!("{incr_duration:.0}"), "2 ps");
                } else {
                    let base_string = base_duration.to_string();
                    assert_eq!(format!("{base_duration:.0}"), base_string);
                    assert_eq!(format!("{incr_duration:.0}"), base_string);
                }
            }
        }

        #[test]
        fn fill() {
            for &scale in TimeScale::ALL {
                // Picoseconds are formatted as nanoseconds by default.
                if scale == TimeScale::PicoSec {
                    continue;
                }

                let duration = FineDuration { picos: scale.picos() };
                let suffix = scale.suffix();
                let pad = " ".repeat(8 - suffix.len());

                assert_fmt_eq!("{duration:<2}", "1 {suffix}");
                assert_fmt_eq!("{duration:<10}", "1 {suffix}{pad}");
            }
        }

        #[test]
        fn pico_sec() {
            test(000, "0 ns");

            test(001, "0.001 ns");
            test(010, "0.01 ns");
            test(100, "0.1 ns");

            test(102, "0.102 ns");
            test(120, "0.12 ns");
            test(123, "0.123 ns");
            test(012, "0.012 ns");
        }

        #[test]
        fn nano_sec() {
            test(001_000, "1 ns");
            test(010_000, "10 ns");
            test(100_000, "100 ns");

            test(100_002, "100 ns");
            test(100_020, "100 ns");
            test(100_200, "100.2 ns");
            test(102_000, "102 ns");
            test(120_000, "120 ns");

            test(001_002, "1.002 ns");
            test(001_023, "1.023 ns");
            test(001_234, "1.234 ns");
            test(001_230, "1.23 ns");
            test(001_200, "1.2 ns");
        }

        #[test]
        fn micro_sec() {
            test(001_000_000, "1 µs");
            test(010_000_000, "10 µs");
            test(100_000_000, "100 µs");

            test(100_000_002, "100 µs");
            test(100_000_020, "100 µs");
            test(100_000_200, "100 µs");
            test(100_002_000, "100 µs");
            test(100_020_000, "100 µs");
            test(100_200_000, "100.2 µs");
            test(102_000_000, "102 µs");

            test(120_000_000, "120 µs");
            test(012_000_000, "12 µs");
            test(001_200_000, "1.2 µs");

            test(001_020_000, "1.02 µs");
            test(001_002_000, "1.002 µs");
            test(001_000_200, "1 µs");
            test(001_000_020, "1 µs");
            test(001_000_002, "1 µs");

            test(001_230_000, "1.23 µs");
            test(001_234_000, "1.234 µs");
            test(001_234_500, "1.234 µs");
            test(001_234_560, "1.234 µs");
            test(001_234_567, "1.234 µs");
        }

        #[test]
        fn milli_sec() {
            test(001_000_000_000, "1 ms");
            test(010_000_000_000, "10 ms");
            test(100_000_000_000, "100 ms");
        }

        #[test]
        fn sec() {
            test(picos::SEC, "1 s");
            test(picos::SEC * 10, "10 s");
            test(picos::SEC * 59, "59 s");

            test(picos::MILLIS * 59_999, "59.99 s");
        }

        #[test]
        fn min() {
            test(picos::MIN, "1 m");
            test(picos::MIN * 10, "10 m");
            test(picos::MIN * 59, "59 m");

            test(picos::MILLIS * 3_599_000, "59.98 m");
            test(picos::MILLIS * 3_599_999, "59.99 m");
            test(picos::HOUR - 1, "59.99 m");
        }

        #[test]
        fn hour() {
            test(picos::HOUR, "1 h");
            test(picos::HOUR * 10, "10 h");
            test(picos::HOUR * 23, "23 h");

            test(picos::MILLIS * 86_300_000, "23.97 h");
            test(picos::MILLIS * 86_399_999, "23.99 h");
            test(picos::DAY - 1, "23.99 h");
        }

        #[test]
        fn day() {
            test(picos::DAY, "1 d");

            test(picos::DAY + picos::DAY / 10, "1.1 d");
            test(picos::DAY + picos::DAY / 100, "1.01 d");
            test(picos::DAY + picos::DAY / 1000, "1.001 d");

            test(picos::DAY * 000010, "10 d");
            test(picos::DAY * 000100, "100 d");
            test(picos::DAY * 001000, "1000 d");
            test(picos::DAY * 010000, "10000 d");
            test(picos::DAY * 100000, "100000 d");

            test(u128::MAX / 1000, "3938453320844195178 d");
            test(u128::MAX, "3938453320844195178974 d");
        }
    }
}