fib_sequence/arithmetic/
display.rs1pub fn display(slice: &[u32]) -> String {
2 slice.iter().rev().enumerate()
3 .map(|(index, &num)| {
4 if index == 0 {
5 format!("{}", num)
6 } else {
7 format!("{:09}", num)
8 }
9 })
10 .collect::<Vec<String>>().join("")
11}
12
13#[cfg(test)]
14mod tests {
15
16 use crate::arithmetic::valid;
17 use std::time;
18
19 #[ignore]
20 #[test]
21 fn bench_valid() {
22 let mut value = vec![7u32];
23
24 let chunk = 100;
25 let size = 400;
26
27 let mut lengths: Vec<(u32, u128)> = vec![(0, 0); size];
28 while value.len() < (chunk * size) {
29 let index = value.len() / chunk;
30 let new_value = crate::arithmetic::add(&value, &value);
31
32 let now = time::Instant::now();
33 assert!(valid(&new_value));
34 let elapsed = now.elapsed().as_nanos();
35
36 let (count, time) = lengths[index];
37 lengths[index] = (count+1, time+elapsed);
38
39 value = new_value
40 }
41
42 for (length, &(count, time)) in lengths.iter().enumerate() {
43 println!("[{:3}] {:+.3e}ns (from {})", length, (time as f64) / (count as f64), count)
44 }
45 }
46
47 #[ignore]
48 #[test]
49 fn bench_display() {
50 let mut value = vec![7u32];
51
52 let chunk = 100;
53 let size = 100;
54
55 let mut lengths: Vec<(u32, u128)> = vec![(0, 0); size];
56 while value.len() < (chunk * size) {
57 let index = value.len() / chunk;
58 let new_value = crate::arithmetic::add(&value, &value);
59
60 let now = time::Instant::now();
61 assert!(super::display(&new_value) != "");
62 let elapsed = now.elapsed().as_nanos();
63
64 let (count, time) = lengths[index];
65 lengths[index] = (count+1, time+elapsed);
66
67 value = new_value
68 }
69
70 for (length, &(count, time)) in lengths.iter().enumerate() {
71 println!("[{:3}] {:+.3e}ns (from {})", length, (time as f64) / (count as f64), count)
72 }
73 }
74}