- var_dump: documentation ( source)
- array_intersect_key: documentation ( source)
- microtime: documentation ( source)
- array_flip: documentation ( source)
- call_user_func_array: documentation ( source)
<?php
function one($s, $p)
{
return array_intersect_key($s, array_flip($p));
}
function two($s, $p)
{
foreach($s as $key => $value)
{
if(!array_key_exists($key, $p))
{
unset($s[$key]);
}
}
return $s;
}
function benchmark(callable $function, $args=null, $count=1)
{
$time = microtime(1);
for($i=0; $i<$count; $i++)
{
$result = is_array($args)?
call_user_func_array($function, $args):
call_user_func_array($function);
}
return [
'total_time' => microtime(1) - $time,
'average_time' => (microtime(1) - $time)/$count,
'count' => $count
];
}
$searchParameters = array(
'parameter1' => 'value1',
'parameter2' => 'value2',
'parameter3' => 'value3',
);
$allowedParamters = array('parameter1','parameter2');
echo('array_intersect'.PHP_EOL);
var_dump(benchmark('one', [$searchParameters, $allowedParamters], 1E6));
echo('foreach'.PHP_EOL);
var_dump(benchmark('two', [$searchParameters, $allowedParamters], 1E6));
This script was stopped while abusing our resources