|  | 
|  | 1 | +use divan::black_box; | 
|  | 2 | +use std::collections::{BTreeMap, HashMap}; | 
|  | 3 | + | 
|  | 4 | +fn main() { | 
|  | 5 | +    divan::main(); | 
|  | 6 | +} | 
|  | 7 | + | 
|  | 8 | +#[divan::bench] | 
|  | 9 | +fn add() -> i32 { | 
|  | 10 | +    black_box(2) + black_box(1) | 
|  | 11 | +} | 
|  | 12 | + | 
|  | 13 | +#[divan::bench] | 
|  | 14 | +#[ignore] | 
|  | 15 | +fn sub() -> i32 { | 
|  | 16 | +    black_box(2) - black_box(1) | 
|  | 17 | +} | 
|  | 18 | + | 
|  | 19 | +#[divan::bench] | 
|  | 20 | +fn mul() -> i32 { | 
|  | 21 | +    black_box(2) * black_box(1) | 
|  | 22 | +} | 
|  | 23 | + | 
|  | 24 | +#[divan::bench] | 
|  | 25 | +fn div() -> i32 { | 
|  | 26 | +    black_box(2) / black_box(1) | 
|  | 27 | +} | 
|  | 28 | + | 
|  | 29 | +#[divan::bench] | 
|  | 30 | +fn rem() -> i32 { | 
|  | 31 | +    black_box(2) % black_box(1) | 
|  | 32 | +} | 
|  | 33 | + | 
|  | 34 | +// 1, 1, 2, 3, 5, ... | 
|  | 35 | +mod fibonacci { | 
|  | 36 | +    use super::*; | 
|  | 37 | + | 
|  | 38 | +    const VALUES: &[u64] = &[0, 5, 10, 20, 30]; | 
|  | 39 | + | 
|  | 40 | +    // O(n) | 
|  | 41 | +    #[divan::bench(args = VALUES)] | 
|  | 42 | +    fn iterative(n: u64) -> u64 { | 
|  | 43 | +        let mut previous = 1; | 
|  | 44 | +        let mut current = 1; | 
|  | 45 | + | 
|  | 46 | +        for _ in 2..=n { | 
|  | 47 | +            let next = previous + current; | 
|  | 48 | +            previous = current; | 
|  | 49 | +            current = next; | 
|  | 50 | +        } | 
|  | 51 | + | 
|  | 52 | +        current | 
|  | 53 | +    } | 
|  | 54 | + | 
|  | 55 | +    // O(2^n) | 
|  | 56 | +    #[divan::bench(args = VALUES, max_time = 1)] | 
|  | 57 | +    fn recursive(n: u64) -> u64 { | 
|  | 58 | +        if n <= 1 { | 
|  | 59 | +            1 | 
|  | 60 | +        } else { | 
|  | 61 | +            recursive(n - 2) + recursive(n - 1) | 
|  | 62 | +        } | 
|  | 63 | +    } | 
|  | 64 | + | 
|  | 65 | +    #[allow(dead_code)] | 
|  | 66 | +    trait Map: Default { | 
|  | 67 | +        fn get(&self, key: u64) -> Option<u64>; | 
|  | 68 | +        fn set(&mut self, key: u64, value: u64); | 
|  | 69 | +    } | 
|  | 70 | + | 
|  | 71 | +    impl Map for HashMap<u64, u64> { | 
|  | 72 | +        fn get(&self, key: u64) -> Option<u64> { | 
|  | 73 | +            self.get(&key).copied() | 
|  | 74 | +        } | 
|  | 75 | + | 
|  | 76 | +        fn set(&mut self, key: u64, value: u64) { | 
|  | 77 | +            self.insert(key, value); | 
|  | 78 | +        } | 
|  | 79 | +    } | 
|  | 80 | + | 
|  | 81 | +    impl Map for BTreeMap<u64, u64> { | 
|  | 82 | +        fn get(&self, key: u64) -> Option<u64> { | 
|  | 83 | +            self.get(&key).copied() | 
|  | 84 | +        } | 
|  | 85 | + | 
|  | 86 | +        fn set(&mut self, key: u64, value: u64) { | 
|  | 87 | +            self.insert(key, value); | 
|  | 88 | +        } | 
|  | 89 | +    } | 
|  | 90 | + | 
|  | 91 | +    // Will be ignored in instrumented mode as we do not support type generics yet | 
|  | 92 | +    // O(n) | 
|  | 93 | +    #[cfg(not(codspeed))] | 
|  | 94 | +    #[divan::bench( | 
|  | 95 | +        types = [BTreeMap<u64, u64>, HashMap<u64, u64>], | 
|  | 96 | +        args = VALUES, | 
|  | 97 | +    )] | 
|  | 98 | +    fn recursive_memoized<M: Map>(n: u64) -> u64 { | 
|  | 99 | +        fn fibonacci<M: Map>(n: u64, cache: &mut M) -> u64 { | 
|  | 100 | +            if let Some(result) = cache.get(n) { | 
|  | 101 | +                return result; | 
|  | 102 | +            } | 
|  | 103 | + | 
|  | 104 | +            if n <= 1 { | 
|  | 105 | +                return 1; | 
|  | 106 | +            } | 
|  | 107 | + | 
|  | 108 | +            let result = fibonacci(n - 2, cache) + fibonacci(n - 1, cache); | 
|  | 109 | +            cache.set(n, result); | 
|  | 110 | +            result | 
|  | 111 | +        } | 
|  | 112 | + | 
|  | 113 | +        fibonacci(n, &mut M::default()) | 
|  | 114 | +    } | 
|  | 115 | +} | 
0 commit comments