-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_array_unique.php
35 lines (28 loc) · 990 Bytes
/
test_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
<?php
// require vender autoload
require_once('vendor/autoload.php');
// add namespace at the top
use Performance\Performance;
// preparation
$max = 5000000;
$a1 = range(1,$max,3);
$a2 = range(1,$max,2);
$a = array_merge($a1,$a2);
// obvious method
Performance::point('array_unique');
$result = array_unique($a);
Performance::finish();
// alternative method
Performance::point('array_keys:array_flip');
$result = array_keys(array_flip($a));
Performance::finish();
// finish all tasks and show test results
Performance::results();
$export = Performance::export();
$points = json_decode($export->toJson())->points;
$p2 = end($points);
$p1 = prev($points);
$percent = round(1-$p2->differenceTime/$p1->differenceTime,4)*100;
$times = round($p1->differenceTime/$p2->differenceTime, 1);
print_r('Alternative method is ' . $percent . '% (' . $times . 'x) faster' . "\n");
print_r('Generated test array with ' . count($a) . ' elements having ' . count($result) . ' unique elements' . "\n");