public function testReport() { Timer::$defaultMinDuration = 100; $report = new Report('Test report', 'Example benchmark.'); $unit = new Unit('String concatenation'); $unit->addClosure(function ($n) { while ($n--) { $x = "foobar {$n}"; } }, '$x = "foobar $n"'); $unit->addClosure(function ($n) { while ($n--) { $x = 'foobar ' . $n; } }, '$x = \'foobar \' . $n'); $report->unitList->add($unit); $unit2 = new Unit('String concatenation with varying length'); foreach ($unit->getMethods() as $method) { $unit2->addMethod($method); } $unit2->addParam(new Parameter('x', '$p = \'x\'')); $unit2->addParam(new Parameter('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', '$p = \'xxxxx..\'')); $report->unitList->add($unit2); $report->run(); }
public function testUnit() { Timer::$defaultMinDuration = 100; $unit = new Unit('test'); $unit->addMethod(new Method(function ($n, $cost) { if ($cost < 10) { while ($n--) { $x = $n * 2; } } else { while ($n--) { $x = $n * 2; $x = number_format($x); } } }, 'calc', 'calculate stuff')); $unit->addClosure(function ($n, $cost) { while ($n--) { $x = password_hash('xx', PASSWORD_DEFAULT, array('cost' => $cost)); } }, 'hash', 'password_hash'); $unit->addParam(new Parameter(5, 'easy')); $unit->addParam(new Parameter(10, 'hard')); $results = $unit->run(); }
$desc = 'Various open source projects are formatted with common settings. Each repository is checked out `--hard` once before the benchmark is run. `.php_cs` and `.php_cs_cache` files are removed to make php-cs-fixer comparable with other formatters. You can see the command arguments by clicking on the method. Versions used: * `' . implode("`\n* `", $versions) . '`'; $unit = new Unit('Formatting open source projects', $desc); $phormat = Exec::create('php', 'phormat', '-n', '-s'); $unit->addClosure(function ($n, $parameter) use($phormat) { while ($n--) { $phormat->run($parameter); } }, 'phormat', "```bash\n\$ " . $phormat->getCommand() . "\n```"); $phpCsFixer = Exec::create('php-cs-fixer', 'fix', '--dry-run', '--level=psr1'); $unit->addClosure(function ($n, $parameter) use($phpCsFixer) { chdir($parameter); while ($n--) { $phpCsFixer->run($parameter); } chdir(__DIR__); }, 'php-cs-fixer PSR-1', "```bash\n\$ " . $phpCsFixer->getCommand() . "\n```"); $phpCsFixer = Exec::create('php-cs-fixer', 'fix', '--dry-run', '--level=psr2'); $unit->addClosure(function ($n, $parameter) use($phpCsFixer) { chdir($parameter); while ($n--) { $phpCsFixer->run($parameter);
A **binary search** makes use of the fact that the list is sorted by the search needle. The complexity is O(log(n)) with n being the array size. **Iterative searches** are acceptable for small lists and the complexity is O(n). Obviously searching in big lists is quite slow unless you're lucky. TAG ); $sortUnit->addClosure(function ($n, $p) { $list = $p['list']; $needle = $p['needle']; while ($n--) { $result = null; $count = count($list); for ($i = 0; $i < $count; $i++) { if ($list[$i] === $needle) { $result = $i; break; } } if ($list[$result] !== $needle) { throw new \Exception(''); } } }, '`for ($i = 0; $i < $count; $i++) break;`'); $sortUnit->addClosure(function ($n, $p) { $list = $p['list']; $needle = $p['needle']; while ($n--) { $count = count($list); $left = 0; $right = $count - 1;