Advertisement
Guest User

Untitled

a guest
Jul 25th, 2021
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.97 KB | None | 0 0
  1. <?php
  2.  
  3. declare(strict_types = 1);
  4.  
  5. /** @noinspection AutoloadingIssuesInspection */
  6.  
  7. class BenchmarkCase
  8. {
  9.     public static $maxNameLength;
  10.  
  11.     public $callable;
  12.  
  13.     public $iterations = 0;
  14.  
  15.     public $name;
  16.  
  17.     public $timing = 0;
  18.  
  19.     public static function create(string $name, callable $callable): self
  20.     {
  21.         $self           = new self;
  22.         $self->name     = $name;
  23.         $self->callable = $callable;
  24.  
  25.         self::$maxNameLength = max(self::$maxNameLength, strlen($name));
  26.  
  27.         return $self;
  28.     }
  29.  
  30.     public static function printResults(float $initialMs, array $cases): void
  31.     {
  32.         $durationMs = microtime(true) - $initialMs;
  33.  
  34.         printf("\r");
  35.  
  36.         usort($cases, static function (BenchmarkCase $a, BenchmarkCase $b) {
  37.             return $a->timing <=> $b->timing;
  38.         });
  39.  
  40.         $caseFirst = $cases[0]->timing;
  41.  
  42.         /** @var BenchmarkCase $case */
  43.         foreach ($cases as $case) {
  44.             printf("Case %s duration of %.6fs (avg. each %.8fs) +%.2f%%\n",
  45.                 str_pad($case->name, self::$maxNameLength, ' '),
  46.                 $case->timing,
  47.                 bcdiv((string) $case->timing, (string) $case->iterations, 8),
  48.                 (100 / $caseFirst * $case->timing) - 100);
  49.         }
  50.  
  51.         printf("\nTotal duration: %.6fs",
  52.             $durationMs);
  53.     }
  54. }
  55.  
  56. set_error_handler(static function ($severity, $message) {
  57.     throw new ErrorException($message, 0, $severity);
  58. });
  59.  
  60. $cases = [
  61.     BenchmarkCase::create('array_merge() inside looping', static function () {
  62.         $arr = [];
  63.  
  64.         for ($i = 0; $i < 100; $i++) {
  65.             $arr = array_merge($arr, [ $i, $i ]);
  66.         }
  67.  
  68.         return $arr;
  69.     }),
  70.     BenchmarkCase::create('array_merge() after looping', static function () {
  71.         $arrArr = [];
  72.  
  73.         for ($i = 0; $i < 100; $i++) {
  74.             $arrArr[] = [ $i, $i ];
  75.         }
  76.  
  77.         return array_merge(... $arrArr);
  78.     }),
  79.     BenchmarkCase::create('array_push() inside looping', static function () {
  80.         $arr = [];
  81.  
  82.         for ($i = 0; $i < 100; $i++) {
  83.             array_push($arr, $i, $i);
  84.         }
  85.  
  86.         return $arr;
  87.     }),
  88. ];
  89.  
  90. const BENCHMARK_DURATION_MS = 90000;
  91. const CYCLES_ITERATION      = 100;
  92.  
  93. $initialMs  = microtime(true);
  94. $stopTiming = $initialMs + BENCHMARK_DURATION_MS / 1000;
  95.  
  96. while (($currentTiming = microtime(true)) < $stopTiming) {
  97.     /** @noinspection NonSecureShuffleUsageInspection */
  98.     shuffle($cases);
  99.  
  100.     printf("Processing: %.2f%%\r", 100 / ($stopTiming - $initialMs) * ($currentTiming - $initialMs));
  101.  
  102.     /** @var BenchmarkCase $case */
  103.     foreach ($cases as $case) {
  104.         $startMs = microtime(true);
  105.  
  106.         for ($j = 0; $j < CYCLES_ITERATION; $j++) {
  107.             call_user_func($case->callable);
  108.         }
  109.  
  110.         $case->iterations += CYCLES_ITERATION;
  111.         $case->timing     += microtime(true) - $startMs;
  112.     }
  113. }
  114.  
  115. BenchmarkCase::printResults($initialMs, $cases);
  116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement