PHP Benchmark

1. For-loop test

Cth. “for ($i=0; $i<$size; $i++)”  dibandingkan “for ($i=0; $i<sizeOf($x); $i++)”

loop 1000 x,

With pre calc – count()                               Total time: 117 µs
Without pre calc – count()                           Total time: 49465 µs
With pre calc – sizeof()                               Total time: 117 µs
Without pre calc – sizeof()                           Total time: 48491 µs
2. Menggunakan =& -ref-operator

$obj = new SomeClass();                            Total time: 522 µs

$obj =& new SomeClass();                          Total time: 546 µs

3. echo VS print

echo ”                                                                    Total time: 87 µs
print ”                                                                    Total time: 101 µs
echo ‘aaaaaaaaaaaaaaaaaaaaaaaaaaaa’                   Total time: 112 µs
print ‘aaaaaaaaaaaaaaaaaaaaaaaaaaaa’                   Total time: 130 µs
echo ‘aaaaaaa’.’aaaaaaa’.’aaaaaaa’.’aaaaaaa’             Total time: 119 µs
echo ‘aaaaaaa’,’aaaaaaa’,’aaaaaaa’,’aaaaaaa’             Total time: 292 µs
print ‘aaaaaaa’.’aaaaaaa’.’aaaaaaa’.’aaaaaaa’             Total time: 146 µs
$a = ‘aaaaaaa’;
echo ‘aaaaaaa’.$a.’aaaaaaa’.$a                                 Total time: 412 µs
$a = ‘aaaaaaa’;
echo ‘aaaaaaa’,$a,’aaaaaaa’,$a                                 Total time: 382 µs
$a = ‘aaaaaaa’;
print ‘aaaaaaa’.$a.’aaaaaaa’.$a                                 Total time: 422 µs
$a = ‘aaaaaaa’;
echo $a.$a.$a.$a                                                     Total time: 408 µs
$a = ‘aaaaaaa’;
echo $a,$a,$a,$a                                                     Total time: 372 µs
$a = ‘aaaaaaa’;
print $a,$a,$a,$a                                                     Total time: 422 µs

4. foreach vs for vs while (list() = each())

Given is a Hash array with 100 elements, 24byte key and 10k data per entry

foreach($aHash as $val);                                          Total time: 11 µs

while(list(,$val) = each($aHash));                            Total time: 78 µs

foreach($aHash as $key => $val);                            Total time: 15 µs

while(list($key,$val) = each($aHash));                      Total time: 93 µs

foreach($aHash as $key=>$val) $tmp[] = $aHash[$key];      Total time: 33 µs

while(list($key) = each($aHash)) $tmp[] = $aHash[$key];     Total time: 93 µs

Get key-/ value-array: foreach($aHash as $key[]=>$val[]);  Total time: 41 µs

Get key-/ value-array: array_keys() / array_values()             Total time: 30 µs

$key = array_keys($aHash);
$size = sizeOf($key);
for ($i=0; $i<$size; $i++) $tmp[] = $aHash[$key[$i]];        Total time: 42 µs

5. for VS while

for($i = 0; $i < 1000000; ++$i);                                      Total time: 58487 µs

for($i = 0; $i < 1000000; ++$i);                                      Total time: 58487 µs

6. Double Quotas vs Single Quotas

Call 1’000x

single (‘) quotes. Just an empty string: $tmp[] = ”;               Total time: 249 µs

double (“) quotes. Just an empty string: $tmp[] = “”;            Total time: 220 µs

7. switch /Case/default VS if/elseif/else

Call 1’000x

if and elseif (using ==)                                                    Total time: 174 µs

if and elseif (using ==)                                                    Total time: 174 µs

if and elseif (using ===)                                                  Total time: 102 µs

if, elseif and else (using ===)                                           Total time: 102 µs

switch / case                                                                   Total time: 160 µs

switch / case / default                                                      Total time: 173 µs

8. foreach vs for vs while (list() = each())

Given again is a Hash array with 100 elements, 24byte key and 10k data per entry.

foreach($aHash as $key=>$val) $aHash[$key] .= “a”;        Total time: 367 µs

while(list($key) = each($aHash)) $aHash[$key] .= “a”;       Total time: 91 µs

$key = array_keys($aHash);
$size = sizeOf($key);
for ($i=0; $i<$size; $i++) $aHash[$key[$i]] .= “a”;          Total time: 38 µs

10. $obj = $someClass->f() vs. $obj =& $someClass->f()

Call 1’000x

$obj = $someClass->f();                Total time: 362 µs

$obj =& $someClass->f();              Total time: 844 µs

Sumber : http://phpbench.com/

1 Comment »

  1. 1
    beli Says:

    I do some code in PHP but I never give to much thought about how to accelerate it. When I find this article about PHP benchmarking I was stunned. I download your solutions and I will most surely try it. Thanks.


    Programmierung Leonberg


RSS Feed for this entry

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: