Blitz inbuild functions much faster than thought
Blitz3D Forums/Blitz3D Beginners Area/Blitz inbuild functions much faster than thought
| ||
Again i stumbled over a little function for calculating distance without the need for a sqr-call. I always thought that these little functions would be computed faster, but here's the code:Graphics3D 800,600 pivot_01 = CreatePivot() pivot_02 = CreatePivot() starttime = MilliSecs() For i=1 To 10000 PositionEntity pivot_01, Rand(-10000,10000),0,rand(-10000,10000) dist = EntityDistanceXZ(pivot_01,pivot_02) ;dist = EntityDistance (pivot_01,pivot_02) Next Text 0,0,MilliSecs() - starttime Flip ;********************************************************************************* ;*** This calculate the 2d distance*distance on y axes ;*** ;********************************************************************************* Function EntityDistanceXZ(Entity1,Entity2) p#=EntityX(Entity2)-EntityX(Entity1) q#=EntityZ(Entity2)-EntityZ(Entity1) Return (p*p)+(q*q) End Function That's happening when you "optimize" your code :-) ------------------------- www.moonworx.de |
| ||
Um, thats great and all, but by optimize do you mean break? Because, your function returns the incorrect answer. Sqr is necessary to calculate distance correctly (although there are some approximations that do not use sqr). You can do this sometimes, but usually only when comparing to another distance that wasnt sqrooted. <edit>ah, nvrmind. I dont have b3d on this computer (just bmax on a flash drive). I guess you mean that entity distance is faster than your algorithm without even using a sqr? |
| ||
I guess you mean that entity distance is faster than your algorithm without even using a sqr? Right, that was the point. For my current needs an aproximation to the real distance was sufficient, but Blitz runs nearly three times faster. ------------------------- www.moonworx.de |
| ||
Probably because you have to call 4 Entityblah() functions rather than looking the values up directly. |