本文整理汇总了PHP中benchmark函数的典型用法代码示例。如果您正苦于以下问题:PHP benchmark函数的具体用法?PHP benchmark怎么用?PHP benchmark使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了benchmark函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: benchmark_many
function benchmark_many($iterations = 10)
{
$res = ['json' => 0, 'yaml' => 0];
for ($i = 0; $i < $iterations; ++$i) {
$stats = benchmark(generate_complex_array());
$res['json'] += $stats['json'];
$res['yaml'] += $stats['yaml'];
}
$res['json'] /= $iterations;
$res['yaml'] /= $iterations;
return $res;
}
示例2: benchmark
function benchmark(Benchmark $benchmark, array $configs, &$result)
{
global $recursion;
$benchmarkCase = getenv('BENCHMARK_CASE') ?: null;
foreach ($configs as $case => $config) {
if ($benchmarkCase === $case || null === $benchmarkCase) {
try {
$result[] = $benchmark->run($config);
} catch (\Doctrine\DBAL\Exception\ConnectionException $ex) {
if ($recursion > 10) {
throw $ex;
}
$recursion++;
trigger_error($ex->getMessage(), E_USER_NOTICE);
trigger_error("Waiting for database ...", E_USER_NOTICE);
sleep(10);
benchmark($benchmark, $configs, $result);
}
}
}
}
示例3: function
* @param $ary
* @param array $keyStack
*/
$iterateWithoutIterator = function (&$ary, $keyStack = array()) {
global $iterateWithoutIterator;
$keyValues = array();
foreach ($ary as $key => $value) {
if (!is_array($value)) {
$nestedKey = implode('.', array_merge($keyStack, array($key)));
$keyValues[$nestedKey] = $value;
} else {
$keyStack2 = $keyStack;
$keyStack2[] = $key;
$keyValues = array_merge($keyValues, $iterateWithoutIterator($value, $keyStack2));
}
}
};
function benchmark($f, $ary, $title = '', $iterations = 10000)
{
echo '<br><b>', $title, '</b><br>';
$start = microtime(true);
for ($i = 0; $i < $iterations; $i++) {
$f($ary);
}
$time = microtime(true) - $start;
echo 'Time: ', $time, '<br>';
echo 'Average: ', $time / $iterations, '<br>';
}
benchmark($iterateWithIterator, $ary, 'With Iterator');
benchmark($iterateWithoutIterator, $ary, 'Without Iterator');
示例4: benchmark
end
ENDIR;
/**
* Compile the function using the JITFU extension!
*
* This is extremely cheap, since no analysis needs to happen. All that happens is the IR
* is turned into JIT-Fu instructions (which are then compiled by libjit).
*/
$func = Jit::getInstance()->compileIrJitFu($ir, 'fibo');
/**
* Now let's benchmark the two implementations!
*
* If you don't have JitFu installed, then both should be equal.
*/
benchmark("fibo", "PHP");
benchmark($func, "ReckiCT");
/**
* A very light weight benchmark tool, which runs the code 100,000 times.
*
* @param callable $func The function to benchmark
* @param string $label The label of the test, for output
*
* @return void
*/
function benchmark(callable $func, $label)
{
$start = microtime(true);
$result = $func(30);
$end = microtime(true);
printf("%s completed fibo(30)=={$result} in %01.4F seconds\n", $label, $end - $start);
}
示例5: benchmark
<?php
require 'vendor/autoload.php';
function benchmark($name, $runs, $function)
{
$start = microtime(true);
while ($runs--) {
$function();
}
$end = microtime(true);
return sprintf('%s: %s', $name, $end - $start) . PHP_EOL;
}
function heavyCalc($varA, $varB)
{
usleep(100);
return $varA + $varB;
}
$memoized = Knlv\memoize('heavyCalc');
echo benchmark('heavyCalc(1, 2)', 100, function () {
heavyCalc(1, 2);
});
echo benchmark('Memoized heavyCalc(1, 2)', 100, function () use(&$memoized) {
$memoized(1, 2);
});
示例6: benchmark
benchmark('invoke', $array, $iterator, $hash, $hashIterator, 'method');
benchmark('last', $array, $iterator, $hash, $hashIterator, function ($v, $k) {
return $v > 1000;
});
benchmark('map', $array, $iterator, $hash, $hashIterator);
benchmark('none', $array, $iterator, $hash, $hashIterator);
benchmark('partition', $array, $iterator, $hash, $hashIterator, function ($v, $k) {
return $v / 2;
});
benchmark('pluck', $array, $iterator, $hash, $hashIterator, 'property');
benchmark('product', $array, $iterator, $hash, $hashIterator, false);
benchmark('ratio', $array, $iterator, $hash, $hashIterator, false);
benchmark('reduce_left', $array, $iterator, $hash, $hashIterator, function ($v) {
return $v;
});
benchmark('reduce_right', $array, $iterator, $hash, $hashIterator, function ($v) {
return $v;
});
benchmark('reject', $array, $iterator, $hash, $hashIterator);
benchmark('select', $array, $iterator, $hash, $hashIterator);
benchmark('some', $array, $iterator, $hash, $hashIterator);
benchmark('sum', $array, $iterator, $hash, $hashIterator, false);
benchmark('difference', $array, $iterator, $hash, $hashIterator, false);
benchmark('ratio', $array, $iterator, $hash, $hashIterator, false);
benchmark('product', $array, $iterator, $hash, $hashIterator, false);
benchmark('average', $array, $iterator, $hash, $hashIterator, false);
benchmark('first_index_of', $array, $iterator, $hash, $hashIterator, 2000);
benchmark('last_index_of', $array, $iterator, $hash, $hashIterator, 2000);
benchmark('head', $array, $iterator, $hash, $hashIterator);
benchmark('tail', $array, $iterator, $hash, $hashIterator);
示例7: html_my_layout
function html_my_layout($vars){ extract($vars);?>
<html>
<head>
<title>Limonde second example</title>
</head>
<body>
<h1>Limonde second example: errors</h1>
<?php echo $content?>
<hr>
<p>
<a href="<?php echo url_for('/')?>">Home</a> |
<a href="<?php echo url_for('/everything/is_going_wrong')?>">everything is going wrong</a> |
<a href="<?php echo url_for('/welcome/')?>">Welcome !</a> |
<a href="<?php echo url_for('/welcome/bill')?>">Welcome Bill ?</a> |
<a href="<?php echo url_for('/welcome/leland')?>">Welcome Leland ?</a> |
<a href="<?php echo url_for('/welcome/bob')?>">Welcome Bob ?</a> |
<a href="<?php echo url_for('/welcome/david')?>">Welcome David ?</a> |
<a href="<?php echo url_for('/welcome/audrey')?>">Welcome Audrey ?</a> |
</p>
</body>
</html>
<!--
<?php print_r(benchmark()); ?>
-->
<?};
示例8: foreach
// Determine the average runtime across all trials.
foreach ($results['t'] as &$result) {
$result /= TRIALS;
}
return $results;
}
$results = [];
echo "Generating benchmarks...\n";
// Run each benchmark in the schedule.
foreach ($schedule as $name => $batches) {
$results[$name] = $results[$name] ?? [];
foreach ($batches as $batchId => $batch) {
$results[$name][$batchId] = [];
list($range, $candidates) = $batch;
if (count($candidates) === 0) {
continue;
}
echo "\n\t{$name}:";
foreach ($candidates as $candidate) {
echo "\n\t\t{$candidate} ";
list($type, $functions) = $config[$name];
$functions = $functions[$candidate];
// Benchmark this task according to its setup, tick and reset functions.
$results[$name][$batchId][$candidate] = benchmark($type, $range, ...$functions);
}
echo "\n";
}
}
// This will be the path that the reporter will use.
$results_path = __DIR__ . '/results.json';
file_put_contents($results_path, json_encode($results, JSON_PRETTY_PRINT));
示例9: isset
<?php
require_once __DIR__ . '/common.php';
$num = isset($argv[1]) ? $argv[1] : 1000;
printf("creating %d test domain objects\n", $num);
benchmark($num, function () {
$rel = new TestRelationship();
$object = new TestObject();
$object->TestRel = $rel;
$rel->save();
$object->save();
});
示例10: foreach
foreach ($args as $arg) {
$total += $arg;
$count++;
}
return $total / $count;
}
function average2(...$args)
{
$total = 0;
$count = 0;
foreach ($args as $arg) {
$total += $arg;
$count++;
}
return $total / $count;
}
$arr = [1, 2, 3, 4, 5];
echo "Step1. func_get_args() vs ... \n";
echo benchmark(function () {
average1(1, 2, 3, 4, 5);
}, 10000), "\n";
echo benchmark(function () {
average2(1, 2, 3, 4, 5);
}, 10000), "\n";
echo "Step2. call_user_func_array() vs ... \n";
echo benchmark(function () use($arr) {
call_user_func_array('average2', $arr);
}, 10000), "\n";
echo benchmark(function () use($arr) {
average2(...$arr);
}, 10000), "\n";
示例11: on_exit
function on_exit()
{
execute_hook('content_end');
benchmark('Everything Done. Will send output to template.php');
benchmark(NULL, true);
$ctrl = global_var('controller');
while ($ctrl !== false) {
$ctrl = substr($ctrl, 0, strrpos($ctrl, '/'));
$template = config('base_path') . 'app/' . $ctrl . '/_template.php';
if (file_exists($template)) {
global_var('template_file', $template, 1);
include $template;
break;
}
}
if (!isset($_SESSION['new_flash'])) {
$_SESSION['new_flash'] = array();
}
$_SESSION['flash'] = $_SESSION['new_flash'];
unset($_SESSION['new_flash']);
execute_hook('exit');
}
示例12: array
$collection = array();
for ($i = 0; $i < 50; $i++) {
$collection[] = createObject();
}
return $collection;
}
function createObject()
{
$post = new \JMS\Serializer\Tests\Fixtures\BlogPost('FooooooooooooooooooooooBAR', new \JMS\Serializer\Tests\Fixtures\Author('Foo'), new \DateTime());
for ($i = 0; $i < 10; $i++) {
$post->addComment(new \JMS\Serializer\Tests\Fixtures\Comment(new \JMS\Serializer\Tests\Fixtures\Author('foo'), 'foobar'));
}
return $post;
}
$serializer = \JMS\Serializer\SerializerBuilder::create()->build();
$collection = createCollection();
$metrics = array();
$f = function () use($serializer, $collection, $format) {
$serializer->serialize($collection, $format);
};
// Load all necessary classes into memory.
benchmark($f, 1);
printf('Benchmarking collection for format "%s".' . PHP_EOL, $format);
$metrics['benchmark-collection-' . $format] = benchmark($f, $iterations);
$output = json_encode(array('metrics' => $metrics));
if (isset($_SERVER['argv'][3])) {
file_put_contents($_SERVER['argv'][3], $output);
echo "Done." . PHP_EOL;
} else {
echo $output . PHP_EOL;
}
示例13: isset
<?php
require_once __DIR__ . '/common.php';
$num = isset($argv[1]) ? $argv[1] : 10000;
$binder = new \Pheasant\Database\Binder();
$binds = array(array('SELECT * FROM table WHERE column=?', array('test')), array('x=?', array('10\'; DROP TABLE --')), array('column1=? and column2=?', array(false, true)), array("name='???' and llamas=?", array(24)), array("name='\\'7r' and llamas=?", array(24)), array("name='\\'7r\\\\' and another='test question?' and llamas=?", array(24)), array("name='\\'7r\\\\' and x='\\'7r' and llamas=?", array(24)));
printf("binding %d statements %d times\n", count($binds), $num);
benchmark($num, function () use($binds, $binder) {
foreach ($binds as $bind) {
$binder->bind($bind[0], $bind[1]);
}
});
示例14: createCollection
}
return $post;
}
$serializer = \Kcs\Serializer\SerializerBuilder::create()->setEventDispatcher(new \Symfony\Component\EventDispatcher\EventDispatcher())->build();
$collection = createCollection();
$metrics = [];
$f = function ($format) use($serializer, $collection) {
$serializer->serialize($collection, $format);
};
// Load all necessary classes into memory.
$f('array');
$table = new \Symfony\Component\Console\Helper\Table($output);
$table->setHeaders(['Format', 'Direction', 'Time']);
$progressBar = new \Symfony\Component\Console\Helper\ProgressBar($output, 8);
$progressBar->start();
foreach (['array', 'json', 'yml', 'xml'] as $format) {
$table->addRow([$format, 'serialize', benchmark($f, $format)]);
$progressBar->advance();
}
$serialized = ['array' => $serializer->serialize($collection, 'array'), 'json' => $serializer->serialize($collection, 'json'), 'yml' => $serializer->serialize($collection, 'yml'), 'xml' => $serializer->serialize($collection, 'xml')];
$type = new \Kcs\Serializer\Type\Type('array', [\Kcs\Serializer\Type\Type::from(\Kcs\Serializer\Tests\Fixtures\BlogPost::class)]);
$d = function ($format) use($serializer, $serialized, $type) {
$serializer->deserialize($serialized[$format], $type, $format);
};
foreach (['array', 'json', 'yml', 'xml'] as $format) {
$table->addRow([$format, 'deserialize', benchmark($d, $format)]);
$progressBar->advance();
}
$progressBar->finish();
$progressBar->clear();
$table->render();
示例15: strtolower
}
$file = $argv[1];
$extension = strtolower(substr($file, strrpos($file, '.') + 1));
$out = tempnam($config['tmp'], 'thumb');
$count = 300;
function benchmark($method)
{
global $config, $file, $extension, $out, $count;
$config['thumb_method'] = $method;
printf("Method: %s\nThumbnailing %d times... ", $method, $count);
$start = microtime(true);
for ($i = 0; $i < $count; $i++) {
$image = new Image($file, $extension);
$thumb = $image->resize($config['thumb_ext'] ? $config['thumb_ext'] : $extension, $config['thumb_width'], $config['thumb_height']);
$thumb->to($out);
$thumb->_destroy();
$image->destroy();
}
$end = microtime(true);
printf("Took %.2f seconds (%.2f/second; %.2f ms)\n", $end - $start, $rate = $count / ($end - $start), 1000 / $rate);
unlink($out);
}
benchmark('gd');
if (extension_loaded('imagick')) {
benchmark('imagick');
} else {
echo "Imagick extension not loaded... skipping.\n";
}
benchmark('convert');
benchmark('gm');
becnhmark('convert+gifsicle');