On 09/14/2015 08:18 PM, Andrei Alexandrescu wrote:
On 09/13/2015 01:16 PM, Daniel N wrote:
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);
}

Apparently that does well with gcc:
http://stackoverflow.com/questions/10996418/efficient-integer-compare-function
-- Andrei


The first version was actually slightly faster when I tested it with std::sort. (i.e. if only "<" is used). I guess it is optimized to a simple integer comparison after inlining. In any case, any performance differences for such simple equivalent functions are random quirks of the respective back-ends.

Reply via email to