(n: usize)
| 34 | /// 只保存前两个值,最节省内存和最快的方式 |
| 35 | #[allow(unused)] |
| 36 | pub fn fib_classic_iteration_loop(n: usize) -> usize { |
| 37 | match n { |
| 38 | 0 => 0, |
| 39 | 1 | 2 => 1, |
| 40 | _ => { |
| 41 | let mut prev = 1; |
| 42 | let mut cur = 1; |
| 43 | let mut i = 3; |
| 44 | loop { |
| 45 | let sum = prev + cur; |
| 46 | prev = cur; |
| 47 | cur = sum; |
| 48 | |
| 49 | i += 1; |
| 50 | if i > n { |
| 51 | return cur; |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /// 只保存前两个值,最节省内存和最快的方式 |
| 59 | /// 但实际基准测试结果并没有预期的那么快, 有可能是for的原因 |
nothing calls this directly
no outgoing calls
no test coverage detected