Skip to content

Instantly share code, notes, and snippets.

@lloc
Created October 1, 2011 11:15
Show Gist options
  • Save lloc/1255901 to your computer and use it in GitHub Desktop.
Save lloc/1255901 to your computer and use it in GitHub Desktop.
Test array_key_exists()
<?php
function getmicrotime () {
list ($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
define ("TESTARRAY_MIN", 1);
define ("TESTARRAY_MAX", 10000);
define ("WINNERARRAY_MAX", 10);
define ("MEASURE_MAX", 10);
srand ((double) microtime() * 1000000);
$j = rand ();
$testArr = array ();
for ($i = TESTARRAY_MIN; $i < TESTARRAY_MAX; $i++) {
$testArr[$i] = $i * $j;
}
srand ((double) microtime () * 1000000);
$winnerArr = array ();
for ($i = 0; $i < WINNERARRAY_MAX; $i++) {
$winnerArr[] = rand (TESTARRAY_MIN, TESTARRAY_MAX);
}
$measureArr = array ();
$trash = 0;
for ($i = 0; $i < MEASURE_MAX; $i++) {
$starttime = getmicrotime ();
foreach ($winnerArr as $key) {
if (array_key_exists ($key, $testArr)) {
$trash = $testArr[$key];
}
}
$measureArr[$i][0] = getmicrotime() - $starttime;
$starttime = getmicrotime ();
foreach ($winnerArr as $key) {
if (isset ($testArr[$key])) {
$trash = $testArr[$key];
}
}
$measureArr[$i][1] = getmicrotime() - $starttime;
}
$average = 0;
foreach ($measureArr as $value) {
$average += $value[0] * 100.00 / $value[1];
}
printf ('%01.2f', ($average / MEASURE_MAX));
?>
@lloc
Copy link
Author

lloc commented Oct 1, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment