[wp-hackers] PHP Coding Practices
Robert Deaton
false.hopes at gmail.com
Wed Feb 15 22:07:54 GMT 2006
On 2/15/06, Chris Lott <chris.lott at gmail.com> wrote:
> On 2/15/06, Mark Jaquith <mark.wordpress at txfx.net> wrote:
http://schogini.us/wordpress/index.php/2005/11/21/tips-to-speed-up-php-code/)
> -- wonder if something else is going on here?
Your tests are not near detailed or run enough times to be a true
indication. Here's something I threw together that might be a more
accurate test, that tries not only real time, but cpu time as well,
because both are very important to us, we need to keep server load
down.
Here's the results with testa.php, which does simple timing. I've run
it three times so you get a good image of the results, and I've
attached the script as testa.txt (I hope txts don't get filtered out)
lappy ~ # php test.php > out
lappy ~ # php test.php >> out
lappy ~ # php test.php >> out
lappy ~ # cat out
strstr at starting point run time: 0.00860
strpos at starting point run time: 0.00824
strstr at mid point run time: 0.00948
strpos at mid point run time: 0.00859
strstr difficult match: 0.01135
strpos difficult match: 0.01045
strstr fail: 0.01075
strpos fail: 0.01057
strstr at starting point run time: 0.00873
strpos at starting point run time: 0.00868
strstr at mid point run time: 0.00907
strpos at mid point run time: 0.00862
strstr difficult match: 0.01142
strpos difficult match: 0.01079
strstr fail: 0.01090
strpos fail: 0.01070
strstr at starting point run time: 0.00866
strpos at starting point run time: 0.00914
strstr at mid point run time: 0.00896
strpos at mid point run time: 0.00884
strstr difficult match: 0.01115
strpos difficult match: 0.01062
strstr fail: 0.01085
strpos fail: 0.01059
As you can see, strstr is slower in all three tests. Next, I did a
test with `time` to see which is faster in system time, run 5 times
each, here are their averages:
strstr (testb.txt):
sys 0m0.006s
strpos (testc.txt):
sys 0m0.003s
So, as you can see, strpos is clearly the faster of the two, digging
into the source of PHP will show you some obvious reasons why. Also,
if anyone is interested on the memory benchmarks of these tests, let
me know.
--
--Robert Deaton
http://somethingunpredictable.com
-------------- next part --------------
<?php
$haystack = 'asimplestring';
$complexhaystack = 'aasasiasimpasimplasimplestringasimplestrng';
$complexhaystack = 'aasasiasimpasimplasimpleasimplestngasimplebringasimplestring';
$start = microtime(true);
for($i = 0; $i < 10000; $i++) {
strstr($haystack, 'asimple');
}
$end = microtime(true);
printf("strstr at starting point run time: %.5f\n", $end - $start);
$start = microtime(true);
for($i = 0; $i < 10000; $i++) {
strpos($haystack, 'asimple');
}
$end = microtime(true);
printf("strpos at starting point run time: %.5f\n", $end - $start);
$start = microtime(true);
for($i = 0; $i < 10000; $i++) {
strstr($haystack, 'mplest');
}
$end = microtime(true);
printf("strstr at mid point run time: %.5f\n", $end - $start);
$start = microtime(true);
for($i = 0; $i < 10000; $i++) {
strpos($haystack, 'mplest');
}
$end = microtime(true);
printf("strpos at mid point run time: %.5f\n", $end - $start);
$start = microtime(true);
for($i = 0; $i < 10000; $i++) {
strstr($complexhaystack, 'asimplestring');
}
$end = microtime(true);
printf("strstr difficult match: %.5f\n", $end - $start);
$start = microtime(true);
for($i = 0; $i < 10000; $i++) {
strpos($complexhaystack, 'asimplestring');
}
$end = microtime(true);
printf("strpos difficult match: %.5f\n", $end - $start);
$start = microtime(true);
for($i = 0; $i < 10000; $i++) {
strstr($complexhaystack, 'asimplettring');
}
$end = microtime(true);
printf("strstr fail: %.5f\n", $end - $start);
$start = microtime(true);
for($i = 0; $i < 10000; $i++) {
strpos($complexhaystack, 'asimplettring');
}
$end = microtime(true);
printf("strpos fail: %.5f\n", $end - $start);
?>
-------------- next part --------------
<?php
$haystack = 'asimplestring';
$complexhaystack = 'aasasiasimpasimplasimpleasimplestngasimplebringasimplestring';
for($i = 0; $i < 10000; $i++) {
strstr($haystack, 'asimple');
}
for($i = 0; $i < 10000; $i++) {
strstr($haystack, 'mplest');
}
for($i = 0; $i < 10000; $i++) {
strstr($complexhaystack, 'asimplestring');
}
for($i = 0; $i < 10000; $i++) {
strstr($complexhaystack, 'asimplettring');
}
?>
-------------- next part --------------
<?php
$haystack = 'asimplestring';
$complexhaystack = 'aasasiasimpasimplasimpleasimplestngasimplebringasimplestring';
for($i = 0; $i < 10000; $i++) {
strpos($haystack, 'asimple');
}
for($i = 0; $i < 10000; $i++) {
strpos($haystack, 'mplest');
}
for($i = 0; $i < 10000; $i++) {
strpos($complexhaystack, 'asimplestring');
}
for($i = 0; $i < 10000; $i++) {
strpos($complexhaystack, 'asimplettring');
}
?>
More information about the wp-hackers
mailing list