Created
June 7, 2013 17:30
-
-
Save betweenbrain/5730931 to your computer and use it in GitHub Desktop.
PHP Session Storage Performance Tests
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
/* | |
* Store random data in array and use in_array() for duplicate checking | |
*/ | |
$then = microtime(TRUE); | |
$i = 1; | |
while ($i <= 1000) { | |
$rand = rand(); | |
$now = microtime(TRUE); | |
if (!in_array($data, $_SESSION[$node])) { | |
$_SESSION[$node][$rand] = array('time' => $now - $then, 'random' => $rand); | |
} | |
$i++; | |
} | |
echo sprintf("Total Elapsed: %f", $now - $then); | |
### Total Elapsed: 0.038185 ### | |
/* | |
* Store random data in array and use isset() for duplicate checking | |
*/ | |
$then = microtime(TRUE); | |
$i = 1; | |
while ($i <= 1000) { | |
$rand = rand(); | |
$now = microtime(TRUE); | |
if (!isset($_SESSION[$node][$rand])) { | |
$_SESSION[$node][$rand] = array('time' => $now - $then, 'random' => $rand); | |
} | |
$i++; | |
} | |
echo sprintf("Total Elapsed: %f", $now - $then); | |
### Total Elapsed: 0.001815 ### | |
/* | |
* Store random data in object and use property_exists() for duplicate checking | |
*/ | |
$then = microtime(TRUE); | |
$i = 1; | |
$_SESSION[$node] = new stdClass(); | |
while ($i <= 1000) { | |
$rand = rand(); | |
$now = microtime(TRUE); | |
if (!property_exists($_SESSION[$node], $rand)) { | |
$_SESSION[$node]->$rand = array('time' => $now - $then, 'random' => $rand); | |
} | |
$i++; | |
} | |
echo sprintf("Total Elapsed: %f", $now - $then); | |
### Total Elapsed: 0.003383 ### |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment