LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-16-2008, 03:28 PM   #1
Zeno McDohl
Member
 
Registered: Apr 2005
Location: Saratoga, NY
Distribution: Slackware
Posts: 322

Rep: Reputation: 30
PHP: more efficient (if else vs switch)


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?

The code is something like this:
Code:
if(strstr($_SERVER['REQUEST_URI'], "catid=2569&"))
{
echo '<script type="text/javascript" language="javascript" src="components/com_ijoomla_ad_agency/ijoomla_ad_agency_zone.php?zid=461"></script>';
}//endif for strstr 2569
else if(strstr($_SERVER['REQUEST_URI'], "catid=2570&"))
{
echo '<script type="text/javascript" language="javascript" src="components/com_ijoomla_ad_agency/ijoomla_ad_agency_zone.php?zid=462"></script>';
}//endif for strstr 2570
 
Old 12-16-2008, 05:20 PM   #2
titanium_geek
Senior Member
 
Registered: May 2002
Location: Horsham Australia
Distribution: elementary os 5.1
Posts: 2,479

Rep: Reputation: 50
*I am not a php guru

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.

From: Clean and Efficient Coding Technique in PHP
Quote:
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.
Also: http://rob.sun3.org/php-code/switch-vs-if/

TG

Last edited by titanium_geek; 12-16-2008 at 05:26 PM. Reason: posted before research. heh.
 
Old 12-16-2008, 05:26 PM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
Old 12-16-2008, 05:53 PM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
Old 12-17-2008, 12:07 PM   #5
Zeno McDohl
Member
 
Registered: Apr 2005
Location: Saratoga, NY
Distribution: Slackware
Posts: 322

Original Poster
Rep: Reputation: 30
[17-Dec-2008 12:45:54] PHP Fatal error: Call to undefined function: memory_get_usage()

Took out all the checks on URI by storing it in a var. Still getting memory exhausted.

How would I use preg_grep on a string? Doesn't seem easy to find examples and everything deals with arrays.

Oh and there are plenty more ifchecks in the same format. Wasn't going to paste everything.

Last edited by Zeno McDohl; 12-17-2008 at 12:11 PM.
 
Old 12-17-2008, 12:22 PM   #6
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
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.

Last edited by jiml8; 12-17-2008 at 12:24 PM.
 
Old 12-17-2008, 12:24 PM   #7
Zeno McDohl
Member
 
Registered: Apr 2005
Location: Saratoga, NY
Distribution: Slackware
Posts: 322

Original Poster
Rep: Reputation: 30
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.
 
Old 12-17-2008, 12:30 PM   #8
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
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.
 
Old 12-17-2008, 12:38 PM   #9
Zeno McDohl
Member
 
Registered: Apr 2005
Location: Saratoga, NY
Distribution: Slackware
Posts: 322

Original Poster
Rep: Reputation: 30
A database was the final step, just wanted to get a quick fix done today.

Thanks though, gonna work on the db.

And shouldn't I be using preg_match, not preg_grep? Regardless, trying to think up a regex that would get the ID from the URI.

Last edited by Zeno McDohl; 12-17-2008 at 12:48 PM.
 
Old 12-17-2008, 01:27 PM   #10
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
Seems to me that putting a database in immediately is a quicker fix than writing out thousands of else if statements.
 
Old 12-17-2008, 01:30 PM   #11
Zeno McDohl
Member
 
Registered: Apr 2005
Location: Saratoga, NY
Distribution: Slackware
Posts: 322

Original Poster
Rep: Reputation: 30
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.
 
Old 12-17-2008, 01:36 PM   #12
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
based on the sample code given:

Code:
$ii = 1;
while ($ii < 2000)
{
    $tmp_catid = $ii + 2108;
    $tmp_catid = "catid=".$tmp_catid;
    if(strstr($_SERVER['REQUEST_URI'], $tmp_catid))
    {
        echo '<script type="text/javascript" language="javascript" src="components/com_ijoomla_ad_agency/ijoomla_ad_agency_zone.php?zid='.$ii.'"></script>';
        $ii = 2000;
    } else {
        $ii = $ii+1;
    }
}
This picks up everything in just a handful of lines.

Last edited by jiml8; 12-17-2008 at 01:39 PM.
 
Old 12-17-2008, 05:18 PM   #13
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
PHP Code:
lookup = array {2569 => 461
               
,2570 =>462
               
,2571 =>463
               
}
echo 
'<script type="text/javascript" language="javascript" src="components/com_ijoomla_ad_agency/ijoomla_ad_agency_zone.php?zid='.lookup[$_GET['catid']].'"></script>'
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Memory-efficient XML parsing in PHP with XMLReader LXer Syndicated Linux News 0 02-04-2007 05:33 PM
cdbackup not efficient! ganninu Linux - General 2 01-18-2007 12:24 PM
[PDF] Is it efficient? Wim Sturkenboom General 8 01-11-2007 04:00 PM
Simple is efficient ???? Maybe/Maybe not ! bigjohn General 21 07-08-2005 10:27 AM
php-mysql-4.2.2-8.0.7 RPM issues, should I switch to redhat 9? jstander Linux - Software 1 05-20-2003 03:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration