Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // benchmarking lookup tables
- // static vs non-static
- // PHP8.0 results are faster with static
- for($j = 1; $j <= 10; $j++) {
- echo "Pass $j", PHP_EOL;
- $start = microtime(true);
- $startMem = memory_get_usage(true);
- for ($i = 0; $i < 1e7; $i++) {
- $test = search('two');
- }
- $end = microtime(true);
- $endMem = memory_get_usage(true);
- echo $end - $start, ' ', $endMem - $startMem, PHP_EOL;
- $start = microtime(true);
- $startMem = memory_get_usage(true);
- for ($i = 0; $i < 1e7; $i++) {
- $test = searchStatic('two');
- }
- $end = microtime(true);
- $endMem = memory_get_usage(true);
- echo $end - $start, ' ', $endMem - $startMem, PHP_EOL;
- }
- function search($value): string {
- $table = [
- 'one' => 'way',
- 'two' => 'ways',
- ];
- return $table[$value] ?? '';
- }
- function searchStatic($value): string {
- static $table = [
- 'one' => 'way',
- 'two' => 'ways',
- ];
- return $table[$value] ?? '';
- }
Add Comment
Please, Sign In to add comment