PHP Performance Bencharmking

To answer again the same question that everyone usually asks, which function is faster, which type is better with PHP programming. The latest data will be updated soon.

Click to go to:

  1. For vs For each
  2. Single Quotes vs Double Quotes

For vs Foreach:

Test 1 (For) Test 2 (For Each) Test 3 (For Each)
Array size: 100000
$c=count($a);
for($i=0;$i<$c;$i++){
    $b++;
}
foreach($a as $k=>$v){
    $b++;
}
foreach($a as $v){
    $b++;
}
Total time
(seconds)
0.041263818740845 0.027344942092896 0.024077892303467
Avg. time 4.1263818740845E-7 2.7344942092896E-7 2.4077892303467E-7
Verdict Winner

Single Quotes vs Double Quotes:

Test 1 (Single Quotes) Test 2 (Double Quotes) Test 3 (Double Quotes)
Iterations: 100000
for($i=0;$i<10000;$i++){
    $b.='a'.$loop.
    '123123123123123123'.implode($a)
    .'tesfasdfadf'.$loop;
}
for($i=0;$i<10000;$i++){
    $b.="a$loop 123123123123123123"
    .implode($a)."tesfasdfadf$loop";
}
for($i=0;$i<10000;$i++){
    $b.="a".$loop
    ." 123123123123123123".
    implode($a)."tesfasdfadf"
    .$loop;
}
Total time
(seconds)
0.24151086807251 0.24391603469849 0.24288511276245
Avg. time 2.4178004264832E-6 2.4391913414001E-6 2.4287104606628E-6
Verdict Winner, yet difference is very small

[] vs array_push:

Test 1 (Single Quotes) Test 2 (Double Quotes)
Iterations: 100000
for($i=0;$i<$loop;$i++){
    $a[]=md5(uniqid(rand(), true));
}
for($i=0;$i<$loop;$i++){
   array_push($a,md5(uniqid(rand(), true)));
}
Total time
(seconds)
6.5702030658722 8.6926040649414
Avg. time 6.5702030658722E-5 8.6926040649414E-5
Verdict Winner

Leave a Reply