optimize vector code

Sascha Katzner sorry.no at spam.invalid
Fri Nov 30 03:30:36 PST 2007


Hi,

I'm currently trying to optimize my vector/matrix code.

the relevant section:
> struct Vector3(T) {
> 	T x, y, z;
> 	void opAddAssign(Vector3 v) {
> 		x += v.x;
> 		y += v.y;
> 		z += v.z;
> 	}
> 	Vector3 opMul(T s) {
> 		return Vector3(x * s, y * s, z * s);
> 	}
> }

If you compare the resulting code from this two examples:

first:
> 	v1 += v2 * 3.0f;
=> 0x59 bytes

second:
> 	v1.x += v2.x * 3.0f;
> 	v1.y += v2.y * 3.0f;
> 	v1.z += v2.z * 3.0f;
=> 0x36 bytes

...it is rather obvious that this is not very good optimized, because 
opMul() creates a new struct in the first example. Is there any Way to 
guide the compiler in the first example to create more efficient code?

LLAP,
Sascha

P.S. attached a complete example of this source
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: vector.d
Url: http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20071130/0900322f/attachment.txt 


More information about the Digitalmars-d-learn mailing list