| 11-19-2007, 07:30 PM | #1 |
Is it? Is it faster to say call "DistanceBetweenPoints" or to use the code it shows in JassCraft which is: JASS:function DistanceBetweenPoints takes location locA, location locB returns real local real dx = GetLocationX(locB) - GetLocationX(locA) local real dy = GetLocationY(locB) - GetLocationY(locA) return SquareRoot(dx * dx + dy * dy) endfunction It seems better to use just the code so you don't have to go through the process of calling the function, so? |
| 11-19-2007, 07:38 PM | #2 |
Yes, the code is faster than the function, due you save the function call which isn't the fastest in WC3. However if the function got around 20+ lines i won't use the code due i'm too lazy to type every damn line ![]() |
| 11-19-2007, 08:53 PM | #3 | |
Quote:
Copy and paste? but thanks that helps. |
| 11-19-2007, 09:02 PM | #4 |
JASS:function DistanceBetweenPoints2 takes x1, y1, x2, y2 returns real local real dx = x2 - x1 local real dy = y2 - y1 return SquareRoot(dx * dx + dy * dy) endfunction |
| 11-19-2007, 09:22 PM | #5 |
JASS:function DistanceBetweenPoints3 takes real dx, real dy returns real return SquareRoot(dx * dx + dy * dy) endfunction Possibly faster than code. |
| 11-19-2007, 09:38 PM | #6 |
Nice one I say. ![]() |
| 11-19-2007, 10:16 PM | #7 |
My rule of thumb is to use function calls when arguments save you local variable declarations for common subexpression elimination. I.e. what griffen demonstrated. |
| 11-19-2007, 11:02 PM | #8 | |
Quote:
Thanks, I'll look through my code to see if i can optimize it that way. |
| 11-19-2007, 11:38 PM | #9 |
don't be afraid of using functions. Except when you need speed. (eg: in a function that runs 40 times a second) In speed-critical code, you should call as few functions as possible. (including natives) This means inlining everything in to a single function. But when speed is not an issue, you should be splitting a lot of code up in to logical functions. (Essentially, if you can modularize a piece of logic that is repeated more than once in to a function, than do so) |
| 11-19-2007, 11:47 PM | #10 | |
Quote:
Of course, I always try to do that. Usually when I see the same lines being repeated there is someway you can change the code to use a lot less space. |
| 11-20-2007, 05:12 AM | #11 |
JASS:function DistanceBetweenPoints4 takes real x1, real y1, real x2, real y2 returns real return SquareRoot((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)) endfunction //! define DistanceBetweenPointsEx(x1,y1,x2,y2) SquareRoot((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)) well, thats wat i do... |
| 11-20-2007, 06:27 AM | #12 |
I used caster system too, but then I stopped since I ran into stupid bugs that didn't happen when I was coding the thing myself. It's better not to use a BJ, but to do everything with natives, because then (in certain situations) you can avoid SquareRoot function which is slow. An example of avoiding SquareRoot can be found here. |
| 11-20-2007, 08:25 AM | #13 |
SquareRoot is not that slow.... considering I've coded a system that calls it... (n*n-n)/2 * 40 times a second and n = 18 actually worked with above 30 FPS... (thats 6120 calls to SquareRoot a second... coupled with a large amount of math and variable manipulation. And other function calls run slightly less often.) Anyway, avoiding BJs is a good thing because they're mostly unnecessary and poorly coded compared to user made functions which do the equivalent. (or just using the natives directly.) Also, user-land function calls are expensive. DoNothing() supposedly takes more time then Sin(). The ONLY times you should go around inlining any useful function is when speed is of the UTMOST importance. Also, don't be afraid to call natives if you actually need to. The speed penalty isn't THAT big, especially when compared to overly complex workarounds. |
| 11-20-2007, 07:05 PM | #14 | |
Quote:
That example looks a lot slower than a simple SquareRoot(). |
| 11-20-2007, 07:46 PM | #15 | |
Quote:
I'll just ignore this if anyone doesn't mind. SquareRoot should be avoided when doing a comparison (which was shown in my example), because instead of square rooting a number, you can just square the number you're comparing it to, which is, of course, faster. x = y is equal to x^2 = y^2 (for example) |
