Making php scripts run as fast as possible

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jaymoore_299
Forum Contributor
Posts: 128
Joined: Wed May 11, 2005 6:40 pm
Contact:

Making php scripts run as fast as possible

Post by jaymoore_299 »

What are some ways to make scripts run as fast as possible? Would encoding them make them run faster and how much faster? Does it help to use as less characters as possible, or does this have an insignificant effect?
Does obfuscating the code and compacting it as much as possible have any effect on the performance?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Zend offers a free Zend Optimizer, or the free PHP accelerator

Have you tried optimizing your code, query calls, database structure?
Perhaps upgrade your server (probably your best bet)
jaymoore_299
Forum Contributor
Posts: 128
Joined: Wed May 11, 2005 6:40 pm
Contact:

Post by jaymoore_299 »

Thanks but I'm looking for something I can optimize at the coding level.

Btw, anyone know if using UNSET() to clear all ununsed variables at the end of the script can help performance?
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

jaymoore_299 wrote:Thanks but I'm looking for something I can optimize at the coding level.

Btw, anyone know if using UNSET() to clear all ununsed variables at the end of the script can help performance?
Once some time back, I proved that using unset() could be detrimental in some cases. And that's just it! It depends on the case we're talking about.

If your script/scripts are pretty small (in terms of how much memory is being used), then the use of unset() won't make a difference at all.

OTOH, if you are dealing with very large data sets, unset() MAY be of benefit, but that depends on how and when it's used. It can also be very, VERY detrimental to performance, depending again on when it's implemented. In the script that I used to benchmark and test unset(), I found that with using unset() on a very large dataset increased the run time 3 fold! ++ungood!!!! However, if I ran it as the script ran and dumped just little portions of that dataset at a regular interval, there was no noticeable gain or loss in performance. This has everything to do with the voodoo mechanics of memory allocation and de-allocation that's being done at a much lower level. Without going into I'll just say that some OS's do a better job of dealing with this then others, but they would all suffer in the example I provided above.

So, where unset is concerned, I would say....

1) Not worth it for small to even medium sized datasets.
2) Do not use to dump a real large dataset. unset() is going to block until it's finished which means everything else in the script is going to sit there and wait until it's done.
3) If you have to deal with large datasets but need to also be concerned with not using too much memory, unset()'ing small portions of it "as you go" will have the least effect on performance, if any.

As for making sure the scripts run faster in general, here's what I do....

1) Only use double quotes in strings when something needs to be evaluated
2) Don't use echo or print for line after line after line for large amounts of text or html. Just switch out of php mode and let Apache (or whatever web server you are using) deal with it. It'll do it faster.
3) Don't use quotes for indexed arrays. I've seen it done and it's braindead!
4) Actually create the variable before using it in an expression of some sort. PHP will continue to run if you don't, but it has to jump through some hoops to figure out what you are after.
5) Unless there is a good reason to post increment, pre increment. In other words, do ++$var instead of $var++. In C, this doesn't seem to matter performance wise, but there is a measureable difference in PHP.
6) If using PHP 4.x, put an ampersand ("&") in front of the "new" vocab word when creating objects.
7) Be mindful of how some data structers are used amongst a series of functions or objects. In those cases where that data structure needs to be seen or accessed by different functions/methods (all those functions/methods are doing is updating small portions of the data set or reading from it), pass it to them by reference. Otherwise you there is at least one copy operation involved, and probably two just to get the data back.

Now some are likely to come behind and accuse me of code smithing. That's fine! As a matter of fact, this kind of concern prolly is not needed the majority of the time with apps that don't see heavy load. But being aware of things like this can be very important in some circumstances. Those nay sayers are ill equiped to deal in those cases.

Cheers
BDKR
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

All the above...;) Keep in mind how to use PHP Referencing in PHP4 - its essential you fully understand the concept when dealing with classes/objects in Object Oriented Programming. There are arguments that some of the above do very little - but its dependent on circumstances, and anyway most developers will do these automatically without thought just in case.
nincha
Forum Contributor
Posts: 191
Joined: Fri Mar 28, 2003 12:30 pm
Location: CA, USA

Post by nincha »

recursive algorithms :P
Post Reply