Saturday, September 22. 2007
Comments
Display comments as
(Linear | Threaded)
Nice benchmarks. But I doubt following those steps will make your site "lightning fast". I thought I was going to read seven magical tips when I clicked the link in my Google reader 
#1
on
2007-09-22 18:59
So, in general, using a non-alias function if faster than using an alias. No surprise there, really.
Strangely enough fputs was faster even though it is an alias to fwrite, according to php.net anyway. Perhaps fwrite is the alias after all, although the speed differences probably have more to do with the storage device not running at constant speed rather than PHP.
Also, the speed differences aren't really that huge
I would have to say that there is more danger in an alias being dropped off of PHP - preventing you from upgrading - than an alias crippling your site.
Strangely enough fputs was faster even though it is an alias to fwrite, according to php.net anyway. Perhaps fwrite is the alias after all, although the speed differences probably have more to do with the storage device not running at constant speed rather than PHP.
Also, the speed differences aren't really that huge
#2
on
2007-09-22 19:45
For those looking for string speed comparisons I have this page (and source) for comparison of various types of string usage.
http://pear.reversefold.com/strings/
http://pear.reversefold.com/strings/
In the PHP internals, an aliased function is just another entry in a hash table. In simplistic terms, the count function is something like this:
function count($arr) {
return $arr->numberOfElements;
}
and since sizeof is an alias, it is effectively:
function sizeof($arr) {
return $arr->numberOfElements;
}
it is not:
function sizeof($arr) {
return count($arr);
}
in other words, there is absolutely no additional overhead for calling an aliased function; it is literally the same code path, with the only difference being the name of the function.
The fact that you are seeing a difference is because your benchmark is "bad".
Ideally, you want to run your benchmarks for several minutes so that you can smooth over various systemic influences. For instance, on your first run, your kernel might need to swap some pages around. On the second run, those pages might be fine. These and other influences can effect pretty big spikes in short lived benchmarks.
I've talked about this in the past: http://netevil.org/blog/2005/sep/benchmarking-in-general
The other thing to consider is this:
When your page takes 60 seconds to load it's time to break out the profiler to find out where the bottleneck is and optimize that, as that will give you the most gain.
http://en.wikipedia.org/wiki/Pareto_principle
function count($arr) {
return $arr->numberOfElements;
}
and since sizeof is an alias, it is effectively:
function sizeof($arr) {
return $arr->numberOfElements;
}
it is not:
function sizeof($arr) {
return count($arr);
}
in other words, there is absolutely no additional overhead for calling an aliased function; it is literally the same code path, with the only difference being the name of the function.
The fact that you are seeing a difference is because your benchmark is "bad".
Ideally, you want to run your benchmarks for several minutes so that you can smooth over various systemic influences. For instance, on your first run, your kernel might need to swap some pages around. On the second run, those pages might be fine. These and other influences can effect pretty big spikes in short lived benchmarks.
I've talked about this in the past: http://netevil.org/blog/2005/sep/benchmarking-in-general
The other thing to consider is this:
When your page takes 60 seconds to load it's time to break out the profiler to find out where the bottleneck is and optimize that, as that will give you the most gain.
http://en.wikipedia.org/wiki/Pareto_principle
When you do benchmarking on Windows you'll always get funny behavior, try using some flavor of Linux where the majority of apps are deployed.
Here is what I got running the tests on my CentOS box.
sizeof vs count
sizeof: 1.18344593048 seconds
count: 1.18188285828 seconds
is_int vs is_integer
is_int: 1.13039302826 seconds
is_integer: 1.14828205109 seconds
and so forth
Here is what I got running the tests on my CentOS box.
sizeof vs count
sizeof: 1.18344593048 seconds
count: 1.18188285828 seconds
is_int vs is_integer
is_int: 1.13039302826 seconds
is_integer: 1.14828205109 seconds
and so forth
Be careful when interpreting the results; these changes aren't going to make your website 10% faster; so you get .3 seconds gain if you run it a million times. But most scripts are running it only once, and the time that count/sizeof is taking compared to the all the other things that are happening is probably tiny.
i'm actually a fans of sizeof instead of count. i just get used.
so now i've to change it. thanks anyway...
so now i've to change it. thanks anyway...
Why are clueless morons allowed into Planet PHP?
#8
on
2007-09-23 11:49
Please see the following post for my reasoning behind this post.
http://torrentialwebdev.com/blog/archives/118-Better-Benchmarks.html
Please direct all comments towards that post.
http://torrentialwebdev.com/blog/archives/118-Better-Benchmarks.html
Please direct all comments towards that post.
The author does not allow comments to this entry

Following one too many posts talking about increasing the speed of PHP scripts by using single quotes instead of double quotes, preincrementing rather than postincrementing variables and the like I wrote 7 tips for lightning fast PHP sites. This post was
Tracked: Sep 23, 21:27