Algorithm performance and throttling in PHP/Apache
May 13, 2006 4:34 AM
Subscribe
Efficient string array comparisons in PHP, and a question about Apache and PHP performance...
I have two arrays of same-length strings that I would like to compare. I have another two arrays that store probabilities ("scores") calculated for those strings. If the two strings match, I need to calculate another value from their respective probabilities. Here's the pseudocode:
run through all values of the string1-array
{
run through all values of the string2-array
{
if string1 matches string2
{
score[string1-index] = function(string1's probability, string2's probability);
remove string2 from string2-array;
remove string2 probability from string2-probability-array;
break;
}
}
remove string1 from string1-array;
remove string1 probability from string1-probability-array;
}
I am working with DNA strings, essentially arrays of up to 4^n values, for length n. For example, for a string length 7, in a worst case scenario, I can be comparing up to 16,384 string1 entries with up to 16,384 string2 entries.
The problem is that my PHP script does not complete this comparison, when the string length is 7 or longer. I either get a blank web page after about 15-20 minutes of work, or an error message that the network connection is broken.
First question: Is there a way to implement this comparison more efficiently — or adjust my PHP 5 or Apache 1.3 configuration to either log at what point exactly this code gets stuck or perhaps allocate more resources to solving this comparison?
I have added the "removal" code after a match to try to shrink down the comparison space on the following round of the loop. With or without this code, the script appears to take the same time +/- a percentage point on comparisons of length n. Probably because of the overhead of resizing the arrays. Comparisons of length 6 and shorter generally take a few minutes or less — I seem to be hitting a resource wall somewhere that drives the CPU to nearly 100% for a long time.
A related problem is that my web server slows to halt while the httpd process appears to allocate 95%+ CPU to this comparison task. I cannot open another session to the PHP application until the current comparison task is finished.
Second, related question: Is there anything I can adjust in PHP 5 or Apache 1.3 to throttle PHP's CPU usage, or any way to code this comparison, so that other users can connect to the website and use the application while this loop does it work?
posted by Mr. Six to computers & internet (16 comments total)
2 users marked this as a favorite
posted by Civil_Disobedient at 6:03 AM on May 13, 2006