| 1 | pub fn change(pct: f64, signed: bool) -> String { |
| 2 | if signed { |
| 3 | format!("{:>+6}%" , signed_short(pct * 1e2)) |
| 4 | } else { |
| 5 | format!("{:>6}%" , short(pct * 1e2)) |
| 6 | } |
| 7 | } |
| 8 | |
| 9 | pub fn time(ns: f64) -> String { |
| 10 | if ns < 1.0 { |
| 11 | format!("{:>6} ps" , short(ns * 1e3)) |
| 12 | } else if ns < 10f64.powi(3) { |
| 13 | format!("{:>6} ns" , short(ns)) |
| 14 | } else if ns < 10f64.powi(6) { |
| 15 | format!("{:>6} µs" , short(ns / 1e3)) |
| 16 | } else if ns < 10f64.powi(9) { |
| 17 | format!("{:>6} ms" , short(ns / 1e6)) |
| 18 | } else { |
| 19 | format!("{:>6} s" , short(ns / 1e9)) |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | pub fn short(n: f64) -> String { |
| 24 | if n < 10.0 { |
| 25 | format!("{:.4}" , n) |
| 26 | } else if n < 100.0 { |
| 27 | format!("{:.3}" , n) |
| 28 | } else if n < 1000.0 { |
| 29 | format!("{:.2}" , n) |
| 30 | } else if n < 10000.0 { |
| 31 | format!("{:.1}" , n) |
| 32 | } else { |
| 33 | format!("{:.0}" , n) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | fn signed_short(n: f64) -> String { |
| 38 | let n_abs = n.abs(); |
| 39 | |
| 40 | if n_abs < 10.0 { |
| 41 | format!("{:+.4}" , n) |
| 42 | } else if n_abs < 100.0 { |
| 43 | format!("{:+.3}" , n) |
| 44 | } else if n_abs < 1000.0 { |
| 45 | format!("{:+.2}" , n) |
| 46 | } else if n_abs < 10000.0 { |
| 47 | format!("{:+.1}" , n) |
| 48 | } else { |
| 49 | format!("{:+.0}" , n) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | pub fn iter_count(iterations: u64) -> String { |
| 54 | if iterations < 10_000 { |
| 55 | format!("{} iterations" , iterations) |
| 56 | } else if iterations < 1_000_000 { |
| 57 | format!("{:.0}k iterations" , (iterations as f64) / 1000.0) |
| 58 | } else if iterations < 10_000_000 { |
| 59 | format!("{:.1}M iterations" , (iterations as f64) / (1000.0 * 1000.0)) |
| 60 | } else if iterations < 1_000_000_000 { |
| 61 | format!("{:.0}M iterations" , (iterations as f64) / (1000.0 * 1000.0)) |
| 62 | } else if iterations < 10_000_000_000 { |
| 63 | format!( |
| 64 | "{:.1}B iterations" , |
| 65 | (iterations as f64) / (1000.0 * 1000.0 * 1000.0) |
| 66 | ) |
| 67 | } else { |
| 68 | format!( |
| 69 | "{:.0}B iterations" , |
| 70 | (iterations as f64) / (1000.0 * 1000.0 * 1000.0) |
| 71 | ) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | pub fn integer(n: f64) -> String { |
| 76 | format!("{}" , n as u64) |
| 77 | } |
| 78 | |
| 79 | #[cfg (test)] |
| 80 | mod test { |
| 81 | use super::*; |
| 82 | |
| 83 | #[test] |
| 84 | fn short_max_len() { |
| 85 | let mut float = 1.0; |
| 86 | while float < 999_999.9 { |
| 87 | let string = short(float); |
| 88 | println!("{}" , string); |
| 89 | assert!(string.len() <= 6); |
| 90 | float *= 2.0; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | #[test] |
| 95 | fn signed_short_max_len() { |
| 96 | let mut float = -1.0; |
| 97 | while float > -999_999.9 { |
| 98 | let string = signed_short(float); |
| 99 | println!("{}" , string); |
| 100 | assert!(string.len() <= 7); |
| 101 | float *= 2.0; |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |