Created
June 22, 2014 15:41
-
-
Save sun/7c15ac1e6c68b1334f49 to your computer and use it in GitHub Desktop.
Bench basename() for namespaced class names
This file contains hidden or 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 | |
# Simple PHP 5.4+ bench script for CLI. | |
const ITERATIONS = 10000; | |
error_reporting(E_ALL | E_STRICT); | |
setlocale(LC_ALL, 'C'); | |
register_shutdown_function(function () { | |
echo PHP_EOL; | |
$memory_peak_end = memory_get_peak_usage(); | |
echo 'Peak memory after test: ', format_kb($memory_peak_end), PHP_EOL; | |
echo 'Memory difference: ', format_kb($memory_peak_end, MEMORY_PEAK_START), PHP_EOL; | |
}); | |
function format_kb($size, $diff_base_size = NULL) { | |
$out = ''; | |
if (isset($diff_base_size)) { | |
$out = ($size = $size - $diff_base_size) > 0 ? '+' : ''; | |
} | |
$out .= number_format($size / 1024) . ' KB'; | |
return $out; | |
} | |
function no_op($text) { | |
return $text; | |
} | |
/** | |
* Initialization. | |
*/ | |
define('MEMORY_PEAK_START', memory_get_peak_usage()); | |
echo 'Peak memory before test: ', format_kb(MEMORY_PEAK_START), PHP_EOL; | |
echo 'Iterations: ', ITERATIONS, PHP_EOL, PHP_EOL; | |
/** | |
* Test cases. | |
*/ | |
$tests['nothing'] = function () { | |
}; | |
$tests['no_op'] = function () { | |
$text = no_op('some string'); | |
}; | |
$classes = ['Sun\Tests\StaticReflection\Fixtures\Example', '\Example', 'Example']; | |
rerun: | |
$class = array_shift($classes); | |
echo '--- ', $class, "\n"; | |
$tests['basename'] = function () use ($class) { | |
return basename($class); | |
}; | |
$tests['explode'] = function () use ($class) { | |
$parts = explode('\\', $class); | |
return end($parts); | |
}; | |
$tests['preg_match'] = function () use ($class) { | |
preg_match('/[^\\\\]+$/', $class, $matches); | |
return $matches[0]; | |
}; | |
$tests['maxnimble/sun'] = function () use ($class) { | |
return array_slice(explode('\\', $class), -1)[0]; | |
}; | |
$tests['maxnimble'] = function () use ($class) { | |
return join('', array_slice(explode('\\', $class), -1)); | |
}; | |
$tests['strrpos'] = function () use ($class) { | |
return ltrim(substr($class, strrpos($class, '\\')), '\\'); | |
}; | |
$tests['strtok'] = function () use ($class) { | |
$token = strtok($class, '\\'); | |
while ($token !== FALSE) { | |
$part = $token; | |
$token = strtok('\\'); | |
} | |
return $part; | |
}; | |
/** | |
* Test runner. | |
*/ | |
$results = array(); | |
foreach ($tests as $name => $test) { | |
$start = microtime(true); | |
for ($i = 0; $i < ITERATIONS; ++$i) { | |
$results[$name] = $test(); | |
} | |
$stop = microtime(true); | |
printf("%-20.20s %2.3f seconds -- %s\n", $name . ':', $stop - $start, var_export($results[$name], TRUE)); | |
} | |
if ($classes) { | |
echo "\n"; | |
goto rerun; | |
} |
This file contains hidden or 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 bench.basename.php | |
Peak memory before test: 151 KB | |
Iterations: 100000 | |
--- Sun\Tests\StaticReflection\Fixtures\Example | |
nothing: 1.122 seconds -- NULL | |
no_op: 1.554 seconds -- NULL | |
basename (Windows): 1.859 seconds -- 'Example' | |
explode: 2.140 seconds -- 'Example' | |
preg_match: 2.274 seconds -- 'Example' | |
maxnimble/sun: 2.161 seconds -- 'Example' | |
maxnimble: 2.598 seconds -- 'Example' | |
strrpos: 2.434 seconds -- 'Example' | |
strtok: 3.851 seconds -- 'Example' | |
--- \Example | |
nothing: 1.125 seconds -- NULL | |
no_op: 1.548 seconds -- NULL | |
basename (Windows): 1.630 seconds -- 'Example' | |
explode: 2.066 seconds -- 'Example' | |
preg_match: 1.802 seconds -- 'Example' | |
maxnimble/sun: 2.111 seconds -- 'Example' | |
maxnimble: 2.581 seconds -- 'Example' | |
strrpos: 2.473 seconds -- 'Example' | |
strtok: 2.104 seconds -- 'Example' | |
--- Example | |
nothing: 1.106 seconds -- NULL | |
no_op: 1.512 seconds -- NULL | |
basename (Windows): 1.634 seconds -- 'Example' | |
explode: 2.061 seconds -- 'Example' | |
preg_match: 1.840 seconds -- 'Example' | |
maxnimble/sun: 2.103 seconds -- 'Example' | |
maxnimble: 2.513 seconds -- 'Example' | |
strrpos: 2.439 seconds -- 'Example' | |
strtok: 2.057 seconds -- 'Example' | |
Peak memory after test: 151 KB | |
Memory difference: +1 KB |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment