<?php
/**
 * @copyright (c) Kufer Software GmbH, Loisachstrasse 4, D-84453 Mühldorf
 */

namespace Kufer\Web\Classes\Addfunctions;

class BenchmarkOperations
{
  private function __construct()
  {
  }

  
  public static function benchmarkOne($fn, ...$args): float
  {
    $start = microtime(true);
    \call_user_func($fn, ...$args);
    $end = microtime(true);

    return ($end - $start) * 1000.0;
  }

  
  public static function benchmarkMultiple(int $repeat, $fn, ...$args): float
  {
    assert($repeat > 0);

    return ((new \Kufer\Web\Classes\Stream\IterableStream(\ArrayOperations::xrange(0, $repeat - 1)))
      ->map(\FunctionOperations::args(BenchmarkOperations::class . '::benchmarkOne', $fn, ...$args))
      ->average());
  }
}