ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have PHP code (rather hackish) to tie together an ad manager and Joomla. It's a bunch of if else's right now and it's obviously inefficient. I'm also starting to get PHP "memory exhausted" errors. If I changed these all to a switch statement, would it help much? Or any suggestions what I should do instead?
You are calling request_uri multiple times- would it be better to pull that in as a static variable for the purposes of this bit of code and then use it over and over again? (again, I am not an efficiency guru.) That way you wouldn't have
"what's the uri? uhh... go. what's the uri? uhh... go." but "what's the uri? this is. go! go! go!"
A point of clarification:
You've got two if/else statements here- that seems pretty reasonable, but do you actually have scads (lots) of the things?
I seem to remember from uni that switches aren't actually much better memory wise than if else statements, but that was java, that was 2 years ago, and there might be some "user brain memory error" there.
Case Statements - A case statement produces roughly 1/5th the amount of machine code as an if/else structure that completes the same task. Use case statements whenever possible. The difference in memory overhead between these two statements is fairly nominal, but the case statement is more efficient to some small degree.
Given that you have it structured as an if else, I cannot imagine that there would be any significant performance difference changing it to a switch. A switch does make it easier to read but that is about all. I would expect that your "memory exhausted" errors are coming from something else and that would be worth investigating do various checks with memory_get_peak_usage() and memory_get_usage() at the start and end of the functions that you suspect to be problematic.
Looking at the code a little more, maybe the repeated use of strstr() in the if statement is a problem (particularly if there are lots of them. You can use preg_grep() to return the matches of a pattern of the URI and then do a switch/ if else on that.
Back to basics. Why do you think the memory error is coming about in this segment of code anyway?
If it were me, I would parse the catid=number string to pull out the number, then use that as an index into a database to pull up the desired zid=. It would lead to a much smaller script and would be far, far easier to maintain.
Because this entire file (ads.php) is full of just these ifchecks. And if I uncomment them all, I get this:
[17-Dec-2008 12:57:01] PHP Parse error: memory exhausted in /home/user/public_html/ads.php on line 5552
I have to comment out the ifchecks from line 5552 and on in order for the code to work.
Last edited by Zeno McDohl; 12-17-2008 at 12:26 PM.
Well, then, I'm guessing that the if statements are creating temp variables that don't go away until the last if statement in the chain.
If you are doing this thousands of times, then you certainly need to find a better way. Use a database, or use indirection and loop over the if statement, filling in the test value each time. Something other than the brute force method you are using.
All I would have needed to do was write one if statement in Excel and double clicked. I have the data in Excel, so using cell refs it would be very quick.
First, getting the catid from the URI, this should be available from the $_GET super variable $_GET['catid']
Second, if you are just performing a lookup from the catid to a zid then a database makes some sense, other alternatives would be to store all the possibilities in an array, record it in a file, but given that you appear to have thousands of them the database would be the most flexible. However, the quick solution would be the array.
You will want to wrap some code around this: check that the catid exists, check that the catid key exists in the lookup array. If the code uses values other than 'catid' from the URI then the code will need to take this into account
Finally, the memory_get_usage() function is available as part of PHP for version 4.3.2 or later, so are you using an older version? If so you may want to consider upgrading the version.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.