[ create a new paste ] login | about

Link: http://codepad.org/5qyvS93x    [ raw code | output | fork ]

PHP, pasted on Mar 31:
<?php

    $array = array_fill(0,50000,'TEST DATA');
    $arraykeyexists_result = array();

    $start = microtime(true);
    for ($i = 0; $i < 100000; $i++) {
        if (array_key_exists($i,$array)) {
            $arraykeyexists_result[] = 1;
        }
        else {
            $arraykeyexists_result[] = 0;
        }
    }
    $arrtime = round(microtime(true)-$start,3);
   
    $start = microtime(true);
    for ($i = 0; $i < 100000; $i++) {
        if (isset($array[$i]) || $array[$i] === null) {
            $arraykeyexists_result[] = 1;
        }
        else {
            $arraykeyexists_result[] = 0;
        }
    }
    $istime = round(microtime(true)-$start,3);
   
    $totaltime = $arrtime+$istime;
    $arrpercentage = round(100*$arrtime/$totaltime,3);
    $ispercentage = round(100*$istime/$totaltime,3);   
   
    echo "array_key_exists(): $arrtime [$arrpercentage%] seconds\n";
    echo "isset():            $istime [$ispercentage%] seconds\n";

?>


Output:
1
2
array_key_exists(): 0.396 [48%] seconds
isset():            0.429 [52%] seconds


Create a new paste based on this one


Comments: