Skip to content

Instantly share code, notes, and snippets.

@bwg
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bwg/ceebeeea96ddd358d309 to your computer and use it in GitHub Desktop.
Save bwg/ceebeeea96ddd358d309 to your computer and use it in GitHub Desktop.
array_diff_key() vs. loop/unset
PHP Version 5.4.30
-------------------------------------------------------
Testing 10 item array over 1,000 iterations:
array_diff_key 1.2109279632568
loop/unset 0.87404251098633
-------------------------------------------------------
Testing 100 item array over 1,000 iterations:
array_diff_key 2.9680728912354
loop/unset 6.4740180969238
-------------------------------------------------------
Testing 1,000 item array over 1,000 iterations:
array_diff_key 18.290042877197
loop/unset 62.99090385437
-------------------------------------------------------
Testing 10,000 item array over 1,000 iterations:
array_diff_key 247.08890914917
loop/unset 610.44406890869
-------------------------------------------------------
Testing 50,000 item array over 1,000 iterations:
array_diff_key 1920.4831123352
loop/unset 3059.1459274292
-------------------------------------------------------
Testing 100,000 item array over 1,000 iterations:
array_diff_key 4492.7201271057
loop/unset 6168.4861183167
-------------------------------------------------------
average (ms) total (ms)
-------------------------------------------------------
array_diff_key 1113.7935320536 6682.7611923218
loop/unset 1651.4025131861 9908.4150791168
<?php
$iterations = 1000;
$sizes = [10, 100, 1000, 10000, 50000, 100000];
$totals = [
'array_diff_key' => 0,
'loop/unset' => 0,
];
echo "PHP Version ".phpversion()."\n\n";
foreach ($sizes as $size) {
$source = array_fill(0, $size, true);
$test = array_filter($source, function($val) {
static $count = 0;
$count++;
return 0 === $count % 2;
});
echo str_repeat('-', 55)."\n";
echo "Testing ".number_format($size)." item array over ".number_format($iterations)." iterations:\n\n";
// -- array_diff_key ------------------------------------------------------
$out = $source;
$start = microtime(true);
for ($i = 0; $i < $iterations; $i++) {
$out = array_diff_key($out, $test);
}
$end = microtime(true);
$duration = ($end - $start) * 1000;
$totals['array_diff_key'] += $duration;
echo str_pad("array_diff_key", 40)."{$duration}\n";
// -- loop/unset ----------------------------------------------------------
$out = $source;
$start = microtime(true);
for ($i = 0; $i < $iterations; $i++) {
foreach ($test as $key => $val) {
unset($out[$key]);
}
}
$end = microtime(true);
$duration = ($end - $start) * 1000;
$totals['loop/unset'] += $duration;
echo str_pad("loop/unset", 40)."{$duration}\n";
}
echo "\n".str_repeat('-', 55)."\n";
echo str_pad('', 20);
echo str_pad('average (ms)', 20);
echo str_pad('total (ms)', 20)."\n";
echo str_repeat('-', 55)."\n";
foreach ($totals as $name => $total) {
echo str_pad($name, 20);
echo str_pad($total/count($sizes), 20);
echo str_pad($total, 20)."\n";
}
echo "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment