PHP BenchmarksIt's a long article, would you like to skip down?
- Postincrement vs. Pretincrement
- Loop limit definition placement
- Str_replace vs. Strtr
- Switch vs. Elseif
- Single switch vs. Single if
- Single quotes vs. Double quotes
- GET/POST vs. REQUEST
- Equal check vs. Identical check
- While vs. For
- For vs. Foreach
- Foreach vs. While/List
- Absolute path vs. Relative path
I was a little uneasy about the results at some of the "benchmark" sites I'd seen. That's why I wrote this page—my own php benchmark.
To start the benchmark project I wrote a comparison function that would test any two commands against each other to see which on was fastest. By default I set the function to run each command through a loop 1,000 times and do that 20 times each to get a relative average and margin of error. The function randomly decides which code to test first in case one test were to influence the other.
After doing many test I found that one thousand iterations for the inside loop resulted in the lowest margin of error. Twenty runs through each loop was also quite sufficient to gather all necessary data.
My function calculates the average speed of a command by adding all twenty benchmarks together then dividing by twenty. It calculates the margin of error as the average variance between the average speed and all the speeds taken. Note that the margin is an absolute value so the speeds themselves can be higher or lower than the average.
To download a copy of the source code that generates the live results page and inputs the data into the database, click here.
Some important considerations include the fact that I employ many optimization features on my server such as the Alternative PHP Cache and Zend. Because some of the optimizations I take effect all my programs, these benchmarks are likely effected. That means this is a great test for me, but the results may vary for you. I should also mention that I'm currently running PHP version 5.2.6.
The final thing my function does is add the value of each benchmark to a database. The results you see on this page are from that database. The reason for this is that running all the benchmarks takes around 53 seconds, and because of my output caching I can't send the page to you until all of the benchmarks are finished. That means you would have to wait 53 seconds at a blank screen hoping it would eventually load. Instead I simply display the stored results and give you the option to run the benchmarks and see the live results in another window by clicking . I encourage everyone to click that link before proceeding. Doing so contributes to the data and gives you a chance to see live results. Besides, you'll be busy reading while the page is loading.
*Note that the counting variable $i exists for all the programs. $i is used to control how many times the test is run, so it goes from 0 to 1000 in each test.
Preincrementing is 8.7% faster than Postincrementing by an average of 0.00034367094736842µs.
Note that $i++ and ++$i are not the same thing. Where the plus signs are relative to the variable establishes when the incrementing will happen. If all you are doing is bumping a variable by one then the two commands are analogous. Check out the official page at http://us3.php.net/manual/en/language.operators.increment.php if you are still unsure what the difference is.
Echo is 0.37% faster than Print by an average of 2.973136842105E-5µs.
Defining the loop limit before the declaration is 68.88% faster than Defining the loop limit in the declaration by an average of 0.04375324631579µs.
strtr is 1.44% faster than str_replace by an average of 0.0002509018947368µs.
Switch is 35.64% faster than Elseif by an average of 0.0088026505263158µs.
Single if is 21.61% faster than Single switch by an average of 0.0020870523880597µs.
Double quotes is 2.9% faster than Single quotes by an average of 0.00024834399999999µs.
Concatenation is 10% faster than Variable replacement by an average of 0.0010401266315789µs.
Single quotes concatenation
Double quotes concatenation
Single quotes concatenation is 5.44% faster than Double quotes concatenation by an average of 0.0005676475789474µs.
GET is 2.84% faster than REQUEST by an average of 0.0003015211578947µs.
POST is 3.29% faster than REQUEST by an average of 0.0003694432631579µs.
Equal check is 4.33% faster than Identical check by an average of 0.0014061810526316µs.
While is 19.71% faster than For by an average of 0.022961865263158µs.
Foreach is 141.29% faster than For by an average of 0.034195844210526µs.
Foreach is 71.98% faster than While/List by an average of 0.019740183157895µs.
Foreach + Keys
While/List + Keys
Foreach + Keys is 64.84% faster than While/List + Keys by an average of 0.020091282105263µs.
Foreach + reset
While + reset
Foreach + reset is 70.68% faster than While + reset by an average of 0.02126794µs.
Foreach + keys + reset
While/list + keys + reset
Foreach + keys + reset is 55.92% faster than While/list + keys + reset by an average of 0.020429498947368µs.
Foreach + keys
Foreach + keys + reset
Foreach + keys is 24.72% faster than Foreach + keys + reset by an average of 0.0072879673684211µs.
While/list + keys
While/list + keys + reset
While/list + keys + reset is 12.16% faster than While/list + keys by an average of 0.0060044084210527µs.
Modify foreach + keys
Modify while/list + keys
Modify foreach + keys is 43.32% faster than Modify while/list + keys by an average of 0.016821869473684µs.
Foreach: complex is 96.99% faster than While/list: complex by an average of 0.018836195789474µs.
Foreach: simple is 85.57% faster than While/list: simple by an average of 0.018057823157895µs.
Foreach: long is 342.88% faster than While/list: long by an average of 0.19179704947368µs.
Foreach: short is 44.43% faster than While/list: short by an average of 0.0088388732631579µs.
Relative path is 22.69% faster than Absolute path by an average of 0.013898927419355µs.
- Post incrementing is slightly faster than preincrementing—but they're not the same.
- Echo is just barely faster than print, but not quite as powerful.
- It's way better to define the loop limit before the loop rather than in it.
- Str_replace is slightly faster than strtr but I guess it takes longer to type...
- Switch is considerably faster than multiple elseifs, but they aren't even close to being the same thing. If you can use switch, go for it. Otherwise don't worry about it.
- Double and single quotes are about the same. Concatenation is quicker and safer than variable replacement, but not as convenient.
- GET—and POST to a lesser extent—are quicker than REQUEST, which I guess makes sense.
- Equal check is quicker than identical check. Again, they're not the same thing, but if you can use either one, stay with ==.
- While and for are each better for certain situations. Speed wise it doesn't make a lick of difference.
- Foreach is way faster than for but they aren't meant to be the same.
- Foreach is always faster than while(list). The longer the array, the more it shows.
- Resetting an array is detrimental to speed when used with foreach, but slightly beneficial when used with while.
At the bottom of the live results page is a table that shows how many times each command is used in my website—just the base page—and how much time I would save by using the efficient version over the inefficient. It then shows how much time I would save per page load, per day(at 200pages/day) and per year. For those of you who didn't see the live results I'll summarize it for you: making all the favorable changes would only save half a second at most of page generation time for every page viewed by every person on this site in a year combined.
That's leads me to what this page is about. Making small changes in the syntax of your code is hardly worth your time. Serverside optimizations like APC,CacheLite,Zend,Mod_GZip and others will save you way way more time than all the little code changes ever would. Also, logical errors often result in tons of lost time. If your page is doing things it doesn't need to be doing, you could be more than doubling your page load time.
A final thing that I'd like to point out is that coding efficiency and readability can often be way more important than these little changes. If it's going to take you an extra second to remember to use echo instead of print, it's better to just use print. While many tricks may speed up coding, an novice coder will have a harder time understanding
So are these results totally useless? Of course not. They're great things to keep in mind and good to know if you are just learning to program in PHP. I wouldn't go back and change any code and I wouldn't spend a bunch of time studying these.