Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- declare(strict_types=1);
- // SET UP
- $iterations = 1000000;
- $x = 'Custom Text';
- $actions = array(
- "sprintf" => function () use ($x) {
- return sprintf('There some string: %s', $x);
- },
- "concat" => function () use ($x) {
- return 'There some string: '. $x;
- },
- "inline" => function () use ($x) {
- return 'There some string: $x';
- },
- );
- // --------------------------------
- function benchmark($name, $iterations, $action)
- {
- $startMemory = memory_get_usage(true);
- $startTime = microtime(true);
- for ($i = 0; $i <= $iterations; ++$i) {
- $a = $action();
- }
- $endTime = microtime(true);
- $endMemory = memory_get_usage(true);
- echo sprintf(
- 'T: %.6f | M: %.2f | %s' . PHP_EOL,
- round($endTime - $startTime, 6),
- round($endMemory - $startMemory, 2),
- $name
- );
- }
- echo '<pre>' . PHP_EOL;
- foreach ($actions as $name => $action) {
- benchmark($name, $iterations, $action);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement