Skip to content

Instantly share code, notes, and snippets.

@vicb
Created January 28, 2013 09:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vicb/4654233 to your computer and use it in GitHub Desktop.
Save vicb/4654233 to your computer and use it in GitHub Desktop.
Doctrine bench
<?php
class Bench
{
public static $cache;
public static $cachem;
public function __construct()
{
self::$cache = array('set' => true, 'tableize_set' => true);
self::$cachem = array('tableize' => array('set' => true));
}
public function keystring($word)
{
$cacheKey = "tableize_" . $word;
if (!isset(static::$cache[$cacheKey])) {
static::$cache[$cacheKey] = true;
}
return static::$cache[$cacheKey];
}
public function keyarray($word)
{
if (!isset(static::$cache[$word])) {
static::$cache[$word] = true;
}
return static::$cache[$word];
}
public function keyarraym($word)
{
if (!isset(static::$cachem['tableize'][$word])) {
static::$cachem['tableize'][$word] = true;
}
return static::$cachem['tableize'][$word];
}
}
$b = new Bench();
$periods = array();
$start = microtime(true);
for ($i = 0; $i < 100000; $i++) {
$b->keystring('set');
}
$end = microtime(true);
$periods[] = $end - $start;
$start = microtime(true);
for ($i = 0; $i < 100000; $i++) {
$b->keyarray('set');
}
$end = microtime(true);
$periods[] = $end - $start;
$start = microtime(true);
for ($i = 0; $i < 100000; $i++) {
$b->keyarraym('set');
}
$end = microtime(true);
$periods[] = $end - $start;
$start = microtime(true);
for ($i = 0; $i < 100000; $i++) {
$b->keystring('not-set');
}
$end = microtime(true);
$periods[] = $end - $start;
$start = microtime(true);
for ($i = 0; $i < 100000; $i++) {
$b->keyarray('not-set');
}
$end = microtime(true);
$periods[] = $end - $start;
$start = microtime(true);
for ($i = 0; $i < 100000; $i++) {
$b->keyarraym('not-set');
}
$end = microtime(true);
$periods[] = $end - $start;
var_dump(array_map(
function ($p) use ($periods) { return $p / $periods[0] * 100; },
$periods
));
array (size=6)
0 => float 100
1 => float 70.533381723979
2 => float 77.080354544914
3 => float 97.932394930246
4 => float 72.422337706221
5 => float 81.855697842132
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment