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(); }
} $version = $name . ' ' . $version; } $report = new Report('Speed comparison of PHP source formatters', '', ['output_dir' => 'benchmark']); $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->addParam(new Parameter($params, 'First of 1000')); // Add different parameters by changing the needle of the haystack $params['needle'] = $list[count($list) - 1]; $unit->addParam(new Parameter($params, 'Last of 1000')); $params['needle'] = '404 Not found'; $unit->addParam(new Parameter($params, 'Not found')); // Finally add this single unit to the report and run it $report->unitList->add($unit); /* * * * * * * * * * * * * * * * * * * * * * * * * * * * */ $sortUnit = new Unit('Searching in a sorted list', <<<'TAG' If all you have is a sorted list, a binary search will do wonders. **Assocative arrays** are still your best friend. Instead of sorting by value, objects are accessed and mapped by value. 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;