I'm confused too, its better to use recursive code than copying and pasting the same lines over and over again.
I think your saying reduce the number of actions which would be correct.
The main problem with using recursion is that the function stack cant be wiped until all recursion levels have finished, making it cost huge amounts of memory compared to non recursive code.
Normally, all local variables of a function are deleted once it returns, which doesnt happen for recursion since each level has to wait for all following recursion levels to finish.
In addition, each function call adds a slight overhead, which can add up if the recursion is very deep.
Quote:
@Mille25
I'm confused too, its better to use recursive code than copying and pasting the same lines over and over again. I think your saying reduce the number of actions which would be correct.
You seem to be confusing loops and recursion. Loops are of course a good thing and should always be used. :)
For everone who wants to get a little bit deeper into this, here is a quick example which shows the difference between recursion and a loop:
As you can see, the recursion version adds up to 6 instaces of the "test" class at the same time, while the loop version just has one instance in memory all the time. :)
I would argue that recursion has its place and is not always useless compared to normal iteration.
In some cases, an iteration solution will be very complicated and messy compared to a recursive solution. In that case, you might want to do it recursively if the memory usage wont cause a problem. This makes the code easier to understand and maintain.
I would argue that recursion has its place and is not always useless compared to normal iteration.
In some cases, an iteration solution will be very complicated and messy compared to a recursive solution. In that case, you might want to do it recursively if the memory usage wont cause a problem. This makes the code easier to understand and maintain.
Absolutly. Recursion can make code much less complex compared to iterative solutions. However, if possible, recursion should be avoided.
Basically, if you're at Level 100, you do this function 100 times to fulfill "Find XP for Level(100)".
Imagine doing this for 14 players.
I aim to reduce memory usage even if it would mean slightly increasing CPU usage. Good idea?
Member since 2010. Made the -The Thing- [Revival] game. Nostalgic of the WC3 days.
Much more efficient and you only run one function at a time in stead of stacking like yours would.
Still alive and kicking, just busy.
My guide to the trigger editor (still a work in progress)
@Vicboy: Go
willuwontu's formula isn't awful, although it will be slightly off compared to your original results, your original if Level were say 5 would be:
50 + 100 + 125 + 150 + 175
willuwontu's yields:
50 + 75 + 100 + 125 + 150
If you aren't so picky about the exact amounts, could just use something as simple as:
return (50 *Level + 50 * (Level - 1));
@Deadzergling: Go
Now i yield the same, didn't realize his was 25 time level, instead of 25 * levels above level 1
Still alive and kicking, just busy.
My guide to the trigger editor (still a work in progress)
Recursive coding should always be avoided if possible.
@Mille25: Go
Why
Member since 2010. Made the -The Thing- [Revival] game. Nostalgic of the WC3 days.
@Vicboy: Go
Because it's inefficient, I guess?
I don't even code, make that brain put in some effort man :P
@Mille25
I'm confused too, its better to use recursive code than copying and pasting the same lines over and over again. I think your saying reduce the number of actions which would be correct.
The main problem with using recursion is that the function stack cant be wiped until all recursion levels have finished, making it cost huge amounts of memory compared to non recursive code.
Normally, all local variables of a function are deleted once it returns, which doesnt happen for recursion since each level has to wait for all following recursion levels to finish.
In addition, each function call adds a slight overhead, which can add up if the recursion is very deep.
You seem to be confusing loops and recursion. Loops are of course a good thing and should always be used. :)
For everone who wants to get a little bit deeper into this, here is a quick example which shows the difference between recursion and a loop:
Code (Warning: Not triggers or .galaxy):
Output:
As you can see, the recursion version adds up to 6 instaces of the "test" class at the same time, while the loop version just has one instance in memory all the time. :)
@Mille25
oh, that makes sense thanks for the info!
I would argue that recursion has its place and is not always useless compared to normal iteration.
In some cases, an iteration solution will be very complicated and messy compared to a recursive solution. In that case, you might want to do it recursively if the memory usage wont cause a problem. This makes the code easier to understand and maintain.
Absolutly. Recursion can make code much less complex compared to iterative solutions. However, if possible, recursion should be avoided.
I guess you could just calculate the value directly, i.e. use something like:
return 12.5 * ( level^2 + 5*level - 2)