Performance Tips

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
gabe
Forum Newbie
Posts: 7
Joined: Thu Apr 18, 2002 4:04 pm
Location: Essex, Ma USA
Contact:

Performance Tips

Post by gabe »

Alright folks, how do you all make your scripts run faster?

-using preg_* functions instead of str_replace()/strstr() and ereg* functions?
-escaping from PHP mode instead of using echo/print, etc?
-using foreach(range(1,10) as $i) instead of for($i=1;$i<10;$i++) ?

I'd like to hear what other folks do to make their code the fastest.
:twisted:
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

using foreach(range(1,10) as $i) instead of for($i=1;$i<10;$i++)
How much faster is that?
using preg_* functions instead of str_replace()/strstr() and ereg* functions
I was always of the mind set that str_replace() was by far faster then both preg_ and ereg_ simply because it didn't have to startup the RegEx engines.

As far as optimizing for speed, well, I start off at the database and server end, optimizing queries and table structures. For PHP, Zend Optimizer or Zend Cache, or all the other optimizers are out there are fairly good.
heschong
Forum Newbie
Posts: 3
Joined: Thu Apr 18, 2002 5:56 pm
Location: Johnson City, TN

Post by heschong »

Right. I'd be interested to know if any of these tips actually hold water.

It is a fact that:

Code: Select all

echo 'foo';
is slightly faster than

Code: Select all

echo "foo";
since single-quoted strings aren't scanned for things like newlines, etc.
gabe
Forum Newbie
Posts: 7
Joined: Thu Apr 18, 2002 4:04 pm
Location: Essex, Ma USA
Contact:

Post by gabe »

heschong wrote:Right. I'd be interested to know if any of these tips actually hold water.
the whole idea of my post was to get other people to contribute their tips as well. those were some examples of mine, though not all are correct after all. i had, in some instances, noted that using the foreach/range combo was faster than the regular for loop. probably just took less time to build and traverse the array of numbers rather than the loop which performs a comparison operation and an increment operation at each interval. i just tested it again and it didn't work any faster. it was marginally slower actually, so perhaps that was a bad example.

str_replace() is definately faster than preg_* and ereg_* but doesn't handle regular expressions. for regexp preg_* is faster.
Zmodem
Forum Commoner
Posts: 84
Joined: Thu Apr 18, 2002 3:59 pm

Post by Zmodem »

heschong wrote: since single-quoted strings aren't scanned for things like newlines, etc.
....and it won't parse (is that the right word?) variables either.

I have heard echo is faster than print.

and yeah, str_replace is much faster the p/ereg* because it has no reg ex's.

I have heard that echo is faster than escaping out all the time, and I have heard the opposite too. So I am not sure which to believe.

One day, I would like to run my own benchmarks, and put this debate to rest once and for all :)
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Between echo() and straight HTML (inline as I call it), the difference is not much. Frankly, I just stay in PHP for the most part, and use echo(), as the performance gain isn't much when you think about everything else. Also, I tend to use template or record set tools, like my Table class to display most everything.

Of course, straight HTML is always going to be faster.

Another tip for those of you out there is to use PHP's functions, and avoid creating your own.

For example, I see too many people recreating functions that already exist in PHP. Sure, you don't know they exists, but you have to search for them, or ask around. I know that even I still find myself forgetting functions every once in a while.

Another thing to look at is database queries. The more queries you can avoid making the better. Of course, database optimization is a whole other ball game.

Another suggestion I can throw out at you may seem strange, but its true: PHP isn't always the best answer. PHP is fast, for sure, but when I want to handle something server side, like a shell script, or something to process large text files, Perl is the way to go.

At the same time, PHP-GTK may seem fun, but other languages out there are built for this type of stuff, like Delphi, VisualBasic (I am going to get stoned for that one), Java, C#, etc.

Then you have other tools such as Pyton, C, and C#. PHP alone is a good thing, but knowing how to use PHP with other tools makes everything work nicer.

Heck, if you are running PHP on a Windows server (For internal Networking, Windows isn't half bad), you have access to COM, and that is a great thing. Imagine your bosses pleasant surprise when the report not only is displayed on the website, but also available as a Excel file (and not just a CSV).

So I guess the point is, PHP optimization sometimes has less to do with optimizing PHP, and more about knowing when to use it, and when to use a better tool.
User avatar
sam
Forum Contributor
Posts: 217
Joined: Thu Apr 18, 2002 11:11 pm
Location: Northern California
Contact:

Post by sam »

Yeah due to the fact that excaped out html is parsed just the same amount as an echo they run at about the same speed, unless you have referance to variables in the excaped html then I beleave that echo will run a slight bit faster.

Cheers Moe
User avatar
Heavy
Forum Contributor
Posts: 478
Joined: Sun Sep 22, 2002 7:36 am
Location: Viksjöfors, Hälsingland, Sweden
Contact:

Post by Heavy »

Waking up an old thread.

I recently optimised some old code that had been changed ... and changed... and changed.
It is about a recursive loop displaying a tree structure from a mysql table where there are (among others) the two columns ID and ParentID.

ParentID of node 1 refers to ID of node 2 where ID of node 2 = ParentID of node 1. This allows for a tree structure (like a filesystem with files and directories) where nodes of the exact same kind can be both children and parents. if ParentID of a node is 0, the node is mounted on root.

At one moment, when I had added some new functionality, it all sacked down, and one execution of the script could take 70 seconds on a 300 MHz AMD-K6 machine.

I started moving things around and splitted the mysql query into two queries, and managed to reduce the query taking 45 seconds to two queries taking 0,01 seconds in total (!)

I Restructured tha data that was internally storing the tree before output and managed to reduce total execution time from 20 seconds to 4 seconds.

Then I started to replace all "strings" with 'strings'. That gave some noticeable boost too, but I don't recall how much. So YES!
heschong wondered, my answer id YES. Single quoted strings are faster, especially if there were parsed variables in the double quoted strings.

Then, I did the same optimizations for som helper functions that were used in the loop and voila!

The result was to read the exact same database content with the same tree view with a 166 MHz Pentium MMX PC doing this taking only 0,3 seconds.

On the 300 MHz AMD-K6, it was as fast as 0,16 seconds.

These times are only times I could measure using PHP. That means, the script parsing time and the packet transport time over the web are not included in these measured time values.
All values are given as an average of ten script executions


Yes it was very very bad at first. But now I'm satisfied, because the page is pretty loaded with functionality and it is only intended as a web application, not a public web portal.

My feelings for PHP has gone like .... in looooooove!
Post Reply