You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The following example shows a benchmark of the iterator adapter step_by(). Once using step_by() directly on the range and once with a redirection via rev().
use test::Bencher;#[bench]fnbench_forward_skip(b:&mutBencher){
b.iter(|| (0..10001).step_by(100).sum::<i32>());}#[bench]fnbench_reverse_skip(b:&mutBencher){
b.iter(|| (0..10001).rev().step_by(100).sum::<i32>());}
Running this benchmark with the current nightly shows these results:
test tests::bench_forward_skip ... bench: 137 ns/iter (+/- 6)
test tests::bench_reverse_skip ... bench: 3,878 ns/iter (+/- 327)
step_by() makes use of nth() of the adapted iterator. A range provides an optimized version of nth(), but by using rev() we get to use the default implementation of nth().
We should Extend std::iter::DoubleEndedIterator to provide a new method maybe nth_back() or rnth() with a default implementation which then can get adapted by rev(). Similar to the already existing next_back(), try_rfold(), rfold() and rfind().
Update:
nth_back() has been merged in #56802. Types which have a specialized nth() and implement DoubleEndedIterator are candidates for a specialized nth_back(). The following list shows these candidates and their implementation status:
The following example shows a benchmark of the iterator adapter
step_by(). Once usingstep_by()directly on the range and once with a redirection viarev().Running this benchmark with the current nightly shows these results:
step_by()makes use ofnth()of the adapted iterator. A range provides an optimized version ofnth(), but by usingrev()we get to use the default implementation ofnth().We should Extend
std::iter::DoubleEndedIteratorto provide a new method maybenth_back()orrnth()with a default implementation which then can get adapted byrev(). Similar to the already existingnext_back(),try_rfold(),rfold()andrfind().Update:
nth_back()has been merged in #56802. Types which have a specializednth()and implementDoubleEndedIteratorare candidates for a specializednth_back(). The following list shows these candidates and their implementation status:nth_backfor ChunksExactMut #63265)