Skip to content

Instantly share code, notes, and snippets.

@betweenbrain
Created June 7, 2013 17:30
Show Gist options
  • Save betweenbrain/5730931 to your computer and use it in GitHub Desktop.
Save betweenbrain/5730931 to your computer and use it in GitHub Desktop.
PHP Session Storage Performance Tests
/*
* 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