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