Zip vs Enumerate

Jordan Wilson wilsonjord at gmail.com
Wed Jun 20 03:44:58 UTC 2018


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


More information about the Digitalmars-d-learn mailing list