-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patharray_flip_vs_array_unique.php
64 lines (51 loc) · 1.69 KB
/
array_flip_vs_array_unique.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php declare(strict_types=1);
require_once __DIR__.'/../vendor/autoload.php';
/*
* You can use an closure or a class that implements TestInterface.
*
* Data that will be processed by the tested function can be executed
* without including its execution time. This will provide more accurate data.
*/
abstract class AbstractBenchmark implements \mre\PHPench\BenchmarkInterface
{
protected $test;
public function setUp($arrSize)
{
$this->test = [];
for ($i = 1; $i < $arrSize; $i++) {
$this->test[$i] = $arrSize % $i;
}
return $this->test;
}
}
class BenchmarkArrayFlip extends AbstractBenchmark
{
public function execute(): void
{
$test = array_flip(array_flip($this->test));
}
}
class BenchmarkArrayUnique extends AbstractBenchmark
{
public function execute(): void
{
$test = array_unique($this->test);
}
}
// Create a new benchmark instance
$phpench = new mre\PHPench(new \mre\PHPench\Aggregator\MedianAggregator());
// Use GnuPlot for output
$oOutput = new \mre\PHPench\Output\GnuPlotOutput('test2.png', 1024, 768);
// Alternatively, print the values to the terminal
//$oOutput = new \mre\PHPench\Output\CliOutput();
$oOutput->setTitle('Compare array_flip and array_unique');
$phpench->setOutput($oOutput);
// Add your test to the instance
$phpench->addBenchmark(new BenchmarkArrayFlip(), 'array_flip');
$phpench->addBenchmark(new BenchmarkArrayUnique(), 'array_unique');
// Run the benchmark and plot the results in realtime.
// With the second parameter you can specify
// the start, end and step for each call
$phpench->setInput(range(1, pow(2, 16), 1024));
$phpench->setRepetitions(4);
$phpench->run();