You are invited to Log in or Register a free Frihost Account!

function that calls itself

I am working on a function that needs to call itself multiple times, and what i wanted to know was just how much something like that may affect performance and speed of the function. may sound confusing so let me try to explain what the function does, it will check an array recursively but when this is done for a multi-dimentional array i have to call the same function again.

It's possible, depending on how many dimensions an array may have, that multiple instances of that function could be running within each other, obviously we're going to have as many simultaneous instances of that function as we have dimensions in our multi-dimensional array so this may be expensive or not i'm not sure.

So again what i want to know is how much something like that may effect the performance of the function. The problem that i have is that i don't know of any good benchmarking tools to try it with various dimensions of multi-dimensional arrays.
shouldn't be a problem, but you could use microtime to see how long it takes to execute the script (or part of it). After 30 sec it will give a fatal error, so try to keep it <20sec to be safe.
for php, usually the only problem with this kind of thing is your script will time out. so you should handle it like rvec said.

performance and speed should not be any problem.
With recursive functions, it is the same as calling different functions for each dimension - so performance will be the same as calling one function inside other. The only thing is the stack. Everytime the function recurses, it saves the current function state and executes the recursive call. Once the inner call is done, the results are returned and outer call state is restored.

For a deep nested recursion, it may take up a bit of memory if you have lot of local variables. If you don't have many local variables and pass your multidimensional array by reference instead of by value, there should be no performance issue at all.
The answer to this question depends on what exactly you are storing in your array and what you are doing with the data. So long as your not copying crazy amounts of data around you should be ok.

Also, to some extent, the number of dimensions you are using will affect this.
In general, it is better to do something iteratively because you reduce the overhead by not requiring the function to be re-initialised with every call as kv said.

Mathematically, every recursive algorithm can be expressed iteratively (ie using a while/for loop) so if it's not too tricky you might want to convert it.
thanks for all your responses everyone, this helped a lot! i will give it a try and let you know...
Related topics
What can recursive function do?
Programming links, info, and tutorials
Fortran Tutorials(77)
java help
A linked list.
Is Lua a suitable script for AI?
switchpic function not being called
Computer Virus Types
Types of computer viruses
javascript function not being seen
Why the english language is so hard to learn
php/HTML pages question
Do we have Free Will or is there only Determinism?
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

© 2005-2011 Frihost, forums powered by phpBB.