Created
October 1, 2011 11:15
-
-
Save lloc/1255901 to your computer and use it in GitHub Desktop.
Test array_key_exists()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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)); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Related post: http://lloc.de/haende-weg-von-array_key_exists.html