On Wednesday, 20 June 2018 at 03:44:58 UTC, Jordan Wilson wrote:
Hello,
Idiomatically, I make use of zip, however, when looking to
speed up my program, notice that using enumerate leads to a
20-30% improvement:
void main(){
auto x = iota(1_000).array;
auto y = iota(1_000).array;
auto func1() {
return zip(x,y).map!(a => a[0]+a[1])
.array;
}
auto func2() {
return x.enumerate
.map!(a => a.value + y[a.index])
.array;
}
assert(func1.equal(func2));
import std.datetime.stopwatch;
auto r = benchmark!(func1, func2)(10_000);
// r[0] approx 1794 ms
// r[1] approx 1295 ms
}
Is there anything I can do to improve zip, before I go ahead
and change to the faster but slightly less readable enumerate?
In my particular case, all ranges that I zip are of the same
length, but not sure how I can leverage that information.
Thanks,
Jordan
This sounds like a very limited case: if you're not zipping
against a iota(foo) then there's no comparing with enumerate,
they simply don't do the same thing at all, and if you are then
it sounds like enumerate expresses the purpose of using an index
way better than a zipped iota IMHO.
Is there anything about your true use case that would be worth
mentionning to better understand your situation?