Skip to content

Instantly share code, notes, and snippets.

@xintron
Created December 19, 2012 15:59
Show Gist options
  • Save xintron/4337783 to your computer and use it in GitHub Desktop.
Save xintron/4337783 to your computer and use it in GitHub Desktop.
<?php
$data = array();
$time = array();
for ($i = 97; $i < 173; $i++)
$data[chr($i)] = $i;
$s = microtime(true);
for ($i = 0; $i < 1000000; $i++) {
isset($data['g']);
}
$time['isset'] = microtime(true)-$s;
printf("isset time:\t\t\t%f\n", $time['isset']);
$s = microtime(true);
for ($i = 0; $i < 1000000; $i++) {
array_key_exists('g', $data);
}
$time['array_key_exists'] = microtime(true)-$s;
printf("array_key_exists time:\t\t%f\n", $time['array_key_exists']);
if ($time['isset'] < $time['array_key_exists'])
printf("isset is faster by:\t\t %.2f%%", (($time['array_key_exists']-$time['isset'])*100/$time['isset']));
else
printf("array_key_exists is faster by:\t %.2f%%", (($time['isset']-$time['array_key_exists'])*100/$time['array_key_exists']));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment