Two ways to compute the last element of an iterator itr
are:
foldl((_, y) -> y, itr)
first(Iterators.drop(itr, length(itr) - 1))
however, the second seems much slower than the first. I wonder why this is the case?
For example:
julia> itr = Iterators.product(1:3000, 2:3000);
julia> @btime foldl((_, y) -> y, $itr)
6.440 ms (0 allocations: 0 bytes)
(3000, 3000)
julia> @btime first(Iterators.drop($it, length($it) - 1))
9.534 ms (0 allocations: 0 bytes)
(3000, 3000)
Iterating over an Iterators.Drop
effectively relies on iterating over the parent, so I am not sure why there’s this difference?