Since then I have periodically doubted the conclusions I drew from my observations. My results still indicate that for Java with the JVM I used, structuring the code into a point structure and implementing the operations as a collection of static operators does make for a significant performance increase.
What I came to doubt was the universality of this observation.
Specifically, I wanted to see if this design methodology also payed off with haxe or if it was more of a JVM implementation artifact.
To test it, I wrote 3 different implementation of a "point" class:
typedef TypedPoint = {
public var x : Float;
public var y : Float;
public var z : Float;
}
class TypedPointOps {
public inline static function add( dest:TypedPoint, a:TypedPoint, b:TypedPoint ) : TypedPoint {
dest.x = a.x + b.x;
dest.y = a.y + b.y;
dest.z = a.z + b.z;
return dest;
}
}
class Point {
public var x : Float;
public var y : Float;
public var z : Float;
public function new( ?x:Float, ?y:Float, ?z:Float ) {
this.assign( x, y, z );
}
public function assign( ?x:Float, ?y:Float, ?z:Float ) : Point {
this.x = x;
this.y = y;
this.z = z;
return this;
}
public function add( a:Point, b:Point ) : Point {
this.x = a.x + b.x;
this.y = a.y + b.y;
this.z = a.z + b.z;
return this;
}
}
I wrote another version called "InlinePoint" which is just like "Point", but used the "inline" keyword.
Here is the result:
No comments:
Post a Comment