Operator overloading or alternatives to expression templates

Daniel N via Digitalmars-d digitalmars-d at puremagic.com
Sun Sep 13 10:16:39 PDT 2015


On Sunday, 13 September 2015 at 14:06:46 UTC, Martin Nowak wrote:
>   struct Foo
>   {
>       size_t id;
>       int opCmp(Foo rhs)
>       {
>           if (id < rhs.id) return -1;
>           if (id == rhs.id) return 0;
>           else return 1;
>       }
>       bool opBinary(string s:"<")(Foo rhs)
>       {
>           return id < rhs.id;
>       }
>   }
>
>   Sorting a million Foos w/ random ids is 37.5% slower with 
> opCmp.
>

Could you try this?

int opCmp(Foo rhs)
{
   return (id > rhs.id) - (id < rhs.id);
}



More information about the Digitalmars-d mailing list