True H&K, but it's all apples and oranges. You can have 2 separate methods or you can have an if statement and take a small hit that you wouldn't notice anyways.
Here's the full experiment. As most of you know I've written/translated a math/vector lib over the years. They've been used a lot by quite a few people and have shown up in many demos. There is one thing I want to point out to Josh though but I'll get to that in a bit.
I'm taking each math method and stuffing it inside a bare bones loop.
While Not KeyHit(KEY_ESCAPE)
Cls
For a = 1 to 1000000
v1.Add( v1, v2 )
Next
'FPS
Show_FPS()
Flip 0
Wend
I do this with each method and do the internal works a few different ways, keeping the fastest iteration.
If I do a straight:
Method Add(a:TVector3)
self.x :+ a.x
self.y :+ a.y
self.z :+ a.z
End Method
I get an FPS of 57 (very slow machine btw). And then I use this as a baseline.
Now, if I do a:
Method Add(a:TVector3, b:TVector3 = Null)
If b = Null Then b = Self
self.x = b.x + a.x
self.y = b.y + a.y
self.z = b.z + a.z
End Method
This way gets 55 fps but allows me to write it either v1.add( v2 ) or v1.add( v2, v3 ). There's a little more versatility there. And I ponder, is the tiny bit more versatility worth 2 fps when cycling it 1 million times? Might seem very trivial to you guys, but I'm writing my final math lib here. It's in the archives and growing almost every day. So now you know what exactly I was trying to do.
If I could set the 2nd argument in the method to self, then I could avoid the 2 fps hit and have the versatility also. ie. Have my cake and eat it too.
To Josh/Halo/Leadwerks: The original BMax math lib I put into the archives is SLOW. Creating a new vector type for each method was a bad call on my part and there's a significant hit with it.
Bad way:
Method Add:Vector( v:Vector )
Local res:Vector = New Vector
res.x = self.x + v.x
res.y = self.y + v.y
res.z = self.z + v.z
Return res
End Method
Best way:
Method Add(v1:Vector, v2:Vector)
self.x = v1.x + v2.x
self.y = v1.x + v2.y
self.z = v1.x + v2.z
End Method
or
Method Add(a:TVector3)
self.x :+ a.x
self.y :+ a.y
self.z :+ a.z
End Method
Doing it the way above yields about a 400% increase in speed.
The bad way above also showed up in a TQuaternion part of MiniB3D if I'm not mistaken. Changing it to the Best Way should speed things up immensely.