View Full Version : Recursion limitations in PHP?
djr33
03-25-2013, 05:22 AM
I'm working on a project for an engineering course with extreme number crunching (this will take hours to run, at a minimum, and that's just one iteration).
I am having trouble writing the code, and I keep writing infinite loops. PHP gives up and throws an error (I've tried a couple servers and the error varies but the behavior is the same-- it gives up).
But now I'm not so sure I'm writing an infinite loop. It's a huge amount of number crunching (something like 17,000^2) and it's giving up. I'd assume I'm writing an infinite loop somewhere but I can't find it. (This code is incredibly frustrating in the first place, so I might be missing it.)
So my questions...
1. How does PHP know when there is an infinite loop without trying it out? How does it know when to give up? It's not a delayed response-- it instantaneously gives up as soon as it gets the request. (And it's not a parse error. I can comment out the recursion and it goes on just fine.)
2. Is this something I can configure? I hesitate to turn it off, but I have a feeling I might have hit the limits of PHP's calculating capabilities.
(And I know PHP is not the ideal language for this; it's just the one I'm most familiar with and this project is a pain, perhaps excessive. Homework! Yay!)
(If anyone is wondering what I'm working on, it's using a Hidden Markov Model to calculate distributional frequencies/probabilities of sounds [=letters] in an input string of language and attempting to uncover "hidden structure". A bit of statistical magic, basically, and I don't fully understand it.)
djr33
03-25-2013, 05:31 AM
Hm. phpinfo() gives this info: pcre.recursion_limit: 100000
According to what I've found with search results, that may be responsible for recursion limitations. But that's kind of riciculous, and I don't think it's actually meant to get to 100,000 levels deep with this code. But it might be. I guess I'll keep playing with it.
james438
03-25-2013, 05:37 AM
Could it be your php.ini value for set_time_limit has been exceeded?
See more here: http://php.net/manual/en/function.set-time-limit.php
djr33
03-25-2013, 05:40 AM
That's the weird thing-- I'm running into two kinds of problems: 1) sometimes it times out; 2) sometimes it refuses to run due to recursion. So (2) is definitely not related to time, because it immediately gives up-- on one server it gives a 500 error; on the other it seems to silently stop at the recursion.
(Unrelated to the problem at hand, I will have to do something about the time limit; in the past I've gotten around it by doing a little bit each time the page runs then reloading the page [automatically, with a redirect]. Or I could just turn that off. In the end I'll probably be only running this locally so as to not anger my host.)
james438
03-25-2013, 05:45 AM
Is there an error message? PHP is also needs processing power to run, so it could be the processing power of the servers is not the best although I'm not sure how to go about testing this.
I just realized that you indicated you are using pcre. With calculations like what you are doing I would recommend outsourcing to Perl. PCRE is extremely processor heavy last I heard and I have not heard any word on that no longer being the case.
djr33
03-25-2013, 05:48 AM
Oops. I meant 500 error in my post above-- a configuration error.
Is there an error message? PHP is also needs processing power to run, so it could be the processing power of the servers is not the best although I'm not sure how to go about testing this.Hm. It seems to be giving up before this could be a problem.
I just realized that you indicated you are using pcre. With calculations like what you are doing I would recommend outsourcing to Perl. PCRE is extremely processor heavy last I heard and I have not heard any word on that no longer being the case.I wouldn't know where to start there, but it would be fine with me to configure it differently. I'm not sure how well that would work on my localhost configuration though-- just the default OSX PHP installation.
I'm not intentionally using PCRE, but I'm not sure how to avoid it either.
james438
03-25-2013, 06:00 AM
Is Perl enabled on your hosting configuration? Do you use deluxe or higher with your GoDaddy account? This would indicate whether it is even possible to use Perl.
The best regular expression experts can be found here: http://regexadvice.com/forums/default.aspx It is not the most advanced website and they are rather strict about posting guidelines, but these people do nothing but regular expressions using all programming languages and not just web design either.
djr33
03-25-2013, 06:08 AM
Is Perl enabled on your hosting configuration? Do you use deluxe or higher with your GoDaddy account? This would indicate whether it is even possible to use Perl.I'll probably be running this on my localhost (macbook) because it'll be running for several hours straight.
The best regular expression experts can be found here: http://regexadvice.com/forums/default.aspx It is not the most advanced website and they are rather strict about posting guidelines, but these people do nothing but regular expressions using all programming languages and not just web design either. I'm not using regex actually. This is all 'for' loops in PHP doing math based on input text. Maybe PCRE only has that limit imposed for regex? If so, that isn't my problem at all. (But it's the only mention of "recursion" in phpinfo().)
djr33
03-25-2013, 08:30 AM
For the record, I've got it working now. I'm not sure what was going wrong before (I'm a bit lost in this code at the moment), but I'll keep playing with it. It's good to check on these things.
Since I use PHP so much for web design, I occasionally use it for other things too and then I run into these odd performance issues when I'm doing something beyond what it's designed for. But usually, with a bit of experimentation, it works.
It *might* have just been a typo in the direction of the loop (subtracting instead of adding; slightly less obvious than that in how I had written it though), but I'm not sure. Working for now.
I also need to remember to not have it output data as it loops-- that seems to slow things down a lot. It was taking about 30 seconds to load the page, but now it's down to 3 when I'm just calculating a value. [And there will be many iterations of this, but that's the base loop at least.]
jscheuer1
03-25-2013, 09:10 AM
As a side note here, just as in any other language which has both, the while loop is more efficient than the for loop. The more iterations of the loop, the more of an efficiency while represents over for.
Also, when incrementing or decrementing a variable:
++$var;
and:
--$var;
are more efficient than:
$var++
and:
$var--
Again, the more iterations, the more efficient.
djr33
03-25-2013, 09:12 AM
Thanks, John. I've seen that before but hadn't ever had the need to take it seriously. In this case, that will be useful.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.