inline functions

Caligo iteronvexor at gmail.com
Sat Mar 26 07:27:30 PDT 2011


I've changed my code since I posted this, so here is something
different that shows performance difference:

module t1;

struct Vector{

private:
  double x = void;
  double y = void;
  double z = void;

public:
  this(in double x, in double y, in double z){
    this.x = x;
    this.y = y;
    this.z = z;
  }

  Vector opBinary(string op)(const double rhs) const if(op == "*"){
    return mixin("Vector(x"~op~"rhs, y"~op~"rhs, z"~op~"rhs)");
  }

  Vector opBinaryRight(string op)(const double lhs) const if(op == "*"){
    return opBinary!op(lhs);
  }
}

void main(){

  auto v1 = Vector(4, 5, 6);
  for(int i = 0; i < 60_000_000; i++){
    v1 = v1 * 1.00000012;
    //v1 = 1.00000012 * v1;
  }
}


Calling opBinaryRight:
/*  gdc -O3 -o t1 t1.d

real    0m0.394s
user    0m0.390s
sys     0m0.000s
*/

Calling opBinary:
/* gdc -O3 -o t1 t1.d

real    0m0.321s
user    0m0.310s
sys     0m0.000s
*/

Those results are best of 10.

There shouldn't be a performance difference between the two, but there.


More information about the Digitalmars-d-learn mailing list