< >

performance-testen-was-das-at-zeichen-macht.php


Quell Code


<?php

error_reporting(E_ALL);
header("content-type: text/plain");
set_time_limit(210);

function microtimediff($a,$b)
{
  list($a_dec,$a_sec)=explode(" ",$a);
  list($b_dec,$b_sec)=explode(" ",$b);
  return $b_sec-$a_sec+$b_dec-$a_dec;
}

test(10);
test(1000);
test(100000);
test(10000000);

function test($max)
{
    echo sprintf("Anzahl Durchläufe: %lu\n",$max);
    
    $arr=array();
    $s=microtime();
    for($i=0;$i<$max;$i++)
    {
        $x=@$arr['undefined'];
    }
    $e=microtime();
    echo sprintf("Fehler-Kontroll-Operator: %3.2f ms\n",microtimediff($s,$e)*1000);
    
    
    $arr=array();
    $s=microtime();
    for($i=0;$i<$max;$i++)
    {
      if(isset($arr['undefined']))
        $x=$arr['undefined'];
      else
        $x=0;
    }
    $e=microtime();
    echo sprintf("  if(isset()) - if-Block: %3.2f ms\n",microtimediff($s,$e)*1000);
    
    
    $arr=array();
    $s=microtime();
    for($i=0;$i<$max;$i++)
    {
        $x=(isset($arr['undefined'])) ? $arr['undefined'] : 0;
    }
    $e=microtime();
    echo sprintf(" if(isset()) - Inline-If: %3.2f ms\n",microtimediff($s,$e)*1000);
    echo "\n";
}

?>