Skip to content

Instantly share code, notes, and snippets.

Created February 3, 2013 12:01
Show Gist options
  • Save anonymous/4701515 to your computer and use it in GitHub Desktop.
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.
<?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());
}
}
?>
@thekid
Copy link

thekid commented Feb 3, 2013

Results:

$ for class in lang.Object xp.compiler.Runner net.xp_lang.tests.RunnerTest ; do echo -n "$class: " ; xp Measure strncmp $class | tail -1 ; done
lang.Object: 100000 iterations, took 0.158 seconds
xp.compiler.Runner: 100000 iterations, took 0.111 seconds
net.xp_lang.tests.RunnerTest: 100000 iterations, took 0.149 seconds

$ for class in lang.Object xp.compiler.Runner net.xp_lang.tests.RunnerTest ; do echo -n "$class: " ; xp Measure preg_match $class | tail -1 ; done
lang.Object: 100000 iterations, took 0.233 seconds
xp.compiler.Runner: 100000 iterations, took 0.144 seconds
net.xp_lang.tests.RunnerTest: 100000 iterations, took 0.244 seconds

$ for class in lang.Object xp.compiler.Runner net.xp_lang.tests.RunnerTest ; do echo -n "$class: " ; xp Measure preg_match_named $class | tail -1 ; done
lang.Object: 100000 iterations, took 0.218 seconds
xp.compiler.Runner: 100000 iterations, took 0.210 seconds
net.xp_lang.tests.RunnerTest: 100000 iterations, took 0.231 seconds

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment