Created
February 3, 2013 12:01
-
-
Save anonymous/4701515 to your computer and use it in GitHub Desktop.
Measure performance of looking up a class in a given package / class loader association.
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 | |
class Measure extends Object { | |
public static function main($args) { | |
$fixtures= [ | |
'strncmp' => [ | |
'provided' => [ | |
'xp.compiler.spi' => 'FileSystemCL<compiler/src/main>', | |
'xp.compiler' => 'FileSystemCL<compiler/src/main>', | |
'net.xp_lang.tests' => 'FileSystemCL<compiler/src/test>' | |
], | |
'test' => function($provided, $class) { | |
foreach ($provided as $package => $loader) { | |
if (0 === strncmp($package, $class, strlen($package))) return $loader; | |
} | |
return NULL; | |
} | |
], | |
'preg_match' => [ | |
'provided' => [ | |
'/^(xp\.compiler\.spi)|(xp\.compiler)/' => 'FileSystemCL<compiler/src/main>', | |
'/^(net\.xp_lang\.tests)/' => 'FileSystemCL<compiler/src/test>' | |
], | |
'test' => function($provided, $class) { | |
foreach ($provided as $pattern => $loader) { | |
if (preg_match($pattern, $class)) return $loader; | |
} | |
return NULL; | |
} | |
], | |
'preg_match_named' => [ | |
'provided' => [ | |
'/^((xp\.compiler\.spi)|(xp\.compiler))|((net\.xp_lang\.tests))/', | |
[ | |
0 => NULL, | |
4 => 'FileSystemCL<compiler/src/main>', | |
6 => 'FileSystemCL<compiler/src/test>' | |
] | |
], | |
'test' => function($provided, $class) { | |
preg_match($provided[0], $class, $matches); | |
return $provided[1][sizeof($matches)]; | |
} | |
], | |
]; | |
$method= isset($args[0]) ? $args[0] : 'strncmp'; | |
$class= isset($args[1]) ? $args[1] : 'xp.compiler.Runner'; | |
if (!isset($fixtures[$method])) { | |
Console::$err->writeLine('*** Unknown method ', $method, ', valid: ', implode(', ', array_keys($fixtures))); | |
return 1; | |
} | |
$fixture= $fixtures[$method]['test']; | |
$provided= $fixtures[$method]['provided']; | |
Console::writeLine('Sample: ', $class, ' in ', $provided); | |
Console::writeLine('Using ', $method, ': ', xp::stringOf($fixture($provided, $class))); | |
if ($e= xp::registry('errors')) { | |
Console::$err->writeLine('*** Method ', $method, ' raised ', $e); | |
return 2; | |
} | |
$t= new \util\profiling\Timer(); | |
$t->start(); | |
for ($i= 0; $i < 100000; $i++) { | |
$fixture($provided, $class); | |
} | |
$t->stop(); | |
Console::writeLinef('%d iterations, took %.3f seconds', $i, $t->elapsedTime()); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Results: