Skip to content

Instantly share code, notes, and snippets.

@bwg
Last active August 29, 2015 14:13

Revisions

  1. bwg revised this gist Jan 9, 2015. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion arraykeytest.php
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,9 @@
    $iterations = 100000;

    $value = [
    ['one' => 1, 'two' => 2, 'three' => 3]
    'one' => 1,
    'two' => 2,
    'three' => 3,
    ];

    $totals = [
  2. bwg revised this gist Jan 9, 2015. 2 changed files with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions arraykeytest.php
    Original file line number Diff line number Diff line change
    @@ -99,3 +99,4 @@




    File renamed without changes.
  3. bwg revised this gist Jan 9, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions arraykeytest.php
    Original file line number Diff line number Diff line change
    @@ -98,3 +98,4 @@




  4. bwg created this gist Jan 9, 2015.
    100 changes: 100 additions & 0 deletions arraykeytest.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,100 @@
    <?php

    $iterations = 100000;

    $value = [
    ['one' => 1, 'two' => 2, 'three' => 3]
    ];

    $totals = [
    'isset_true' => 0,
    'isset_false' => 0,
    'array_key_true' => 0,
    'array_key_false' => 0,
    ];

    echo "PHP Version ".phpversion()."\n\n";

    echo str_repeat('-', 50)."\n";

    //-----------------------------------------------------------------------------

    echo "Testing isset over ".number_format($iterations)." iterations:\n\n";

    $start = microtime(true);

    for ($i = 0; $i < $iterations; $i++) {
    if (isset($value['one'])) {}
    }

    $end = microtime(true);

    $duration = ($end - $start) * 1000;

    $totals['isset_true'] += $duration;

    echo str_pad('isset true', 35)."{$duration}\n";

    $start = microtime(true);

    for ($i = 0; $i < $iterations; $i++) {
    if (isset($value['fail'])) {}
    }

    $end = microtime(true);

    $duration = ($end - $start) * 1000;

    $totals['isset_false'] += $duration;

    echo str_pad('isset false', 35)."{$duration}\n\n";

    //-----------------------------------------------------------------------------

    echo "Testing array_key_exists over ".number_format($iterations)." iterations:\n\n";

    $start = microtime(true);

    for ($i = 0; $i < $iterations; $i++) {
    if (array_key_exists('one', $value)) {}
    }

    $end = microtime(true);

    $duration = ($end - $start) * 1000;

    $totals['array_key_true'] += $duration;

    echo str_pad('array_key_exists true', 35)."{$duration}\n";

    $start = microtime(true);

    for ($i = 0; $i < $iterations; $i++) {
    if (array_key_exists('fail', $value)) {}
    }

    $end = microtime(true);

    $duration = ($end - $start) * 1000;

    $totals['array_key_false'] += $duration;

    echo str_pad('array_key_exists false', 35)."{$duration}\n";

    //-----------------------------------------------------------------------------

    echo "\n".str_repeat('-', 50)."\n";
    echo str_pad('', 35);
    echo str_pad('total (ms)', 20)."\n";
    echo str_repeat('-', 50)."\n";

    foreach ($totals as $name => $total) {
    echo str_pad($name, 35);
    echo str_pad($total, 20)."\n";
    }

    echo "\n";




    20 changes: 20 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    PHP Version 5.6.3

    --------------------------------------------------
    Testing isset over 100,000 iterations:

    isset true 23.11897277832
    isset false 21.745920181274

    Testing array_key_exists over 100,000 iterations:

    array_key_exists true 95.006942749023
    array_key_exists false 88.358879089355

    --------------------------------------------------
    total (ms)
    --------------------------------------------------
    isset_true 23.11897277832
    isset_false 21.745920181274
    array_key_true 95.006942749023
    array_key_false 88.358879089355