# loop-over-inclusive-range-test **Repository Path**: tinusgraglin/loop-over-inclusive-range-test ## Basic Information - **Project Name**: loop-over-inclusive-range-test - **Description**: Testing methods to loop over an inclusive range in Rust. - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-01-06 - **Last Updated**: 2024-01-06 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Introduction This is my simple benchmark comparing different methods to loop over an inclusive number range: 1. `for i in 0..=n` 2. `for i in MyRangeInclusiveIter`, the implemention of `MyRangeInclusiveIter::next` is simply the implemention of `RangeInclusive::next` without the `start > end` check. 3. A loop expansion of 2, somehow its performance is very much different from 2. 4. `while i < n` loop with manual `i == n` case. 5. Manual `i == 0` case and then loop the rest. 6. `(0..=n).for_each` 7. `(0..=n).fold` # Last Result ``` test sum_of_all_good_bench_1 ... bench: 723,901 ns/iter (+/- 18,071) test sum_of_all_good_bench_2 ... bench: 244,559 ns/iter (+/- 7,255) test sum_of_all_good_bench_3 ... bench: 828,042 ns/iter (+/- 23,165) test sum_of_all_good_bench_4 ... bench: 244,446 ns/iter (+/- 14,579) test sum_of_all_good_bench_5 ... bench: 279,080 ns/iter (+/- 2,750) test sum_of_all_good_bench_6 ... bench: 209,503 ns/iter (+/- 23,394) test sum_of_all_good_bench_7 ... bench: 246,574 ns/iter (+/- 10,524) ``` # Conclusion Uses `for_each` or `fold` when looping over an inclusive range.