Skip to content

Instantly share code, notes, and snippets.

@cspray
Created March 15, 2012 00:48
Show Gist options
  • Save cspray/2040791 to your computer and use it in GitHub Desktop.
Save cspray/2040791 to your computer and use it in GitHub Desktop.
OOP storage benchmark omgz
<?php
error_reporting(-1);
class Tester {
protected $something;
public function __construct($something) {
$this->something = $something;
}
}
class IntObjectStorage {
protected $storage = array();
public function getObjectId($add_this_to_it) {
$this->storage[] = new Tester($add_this_to_it);
return count($this->storage) - 1;
}
}
class ObjectGenerator {
protected $storage = array();
public function getObject($put_this_in_constructor) {
$this->storage[] = new Tester($put_this_in_constructor);
$index = count($this->storage) - 1;
return $this->storage[$index];
}
}
$IntObject = new IntObjectStorage();
$ObjectGenerator = new ObjectGenerator();
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$int_start_time = $mtime;
$int_counter = 100000;
for ($i = 0; $i < $int_counter; $i++)
{
$z = $IntObject->getObjectId($i);
}
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$int_end_time = $mtime;
$int_total_time = ($int_end_time - $int_start_time);
echo 'The total elapsed time for creating ' . $int_counter . ' objects stored in an array and returning the index is ' . $int_total_time . '<br />';
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$object_start_time = $mtime;
$object_counter = 100000;
for ($i = 0; $i < $object_counter; $i++) {
$z = $ObjectGenerator->getObject($i);
}
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$object_end_time = $mtime;
$object_total_time = ($object_end_time - $object_start_time);
echo 'The total elapsed time for creating ' . $object_counter . ' objects and returning them directly is ' . $object_total_time . '<br />';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment