Performance of map!()

John Colvin via Digitalmars-d digitalmars-d at puremagic.com
Fri Apr 24 03:53:01 PDT 2015


On Friday, 24 April 2015 at 10:22:05 UTC, Chris wrote:
> I replaced a range that was similar to map with map and the 
> performance dropped by ~0.5 msec.
>
> The range I used previously is based on Adam's D Cookbook. It 
> is consistently faster than map.
>
> private struct Transformer(alias agent, R) if (isInputRange!R) {
>
>   private R r;
>   this (R r) {
>     this.r = r;
>   }
>
>   static if (isInfinite!R) {
>     enum bool empty = false;
>   }
>   else {
>     @property bool empty() {
>       return r.empty;
>     }
>   }
>
>   void popFront() {
>     r.popFront();
>
>   }
>   @property ref auto front() {
>     return agent(r.front);
>   }
>
>   static if (isForwardRange!R) {
>     @property auto save() {
>       return Transformer!(agent, R)(r.save);
>     }
>   }
>
>   static if (isBidirectionalRange!R) {
>     @property auto back() {
>       return agent(r.back);
>     }
>     void popBack() {
>       r.popBack();
>     }
>   }
>
>   static if (isRandomAccessRange!R) {
>     auto opIndex(size_t i) {
>       return agent(r[i]);
>     }
>   }
>
>   static if (hasLength!R) {
>     @property auto length() {
>       return r.length;
>     }
>   }
> }

Compiler? Compiler flags?


More information about the Digitalmars-d mailing list