forked from php/php-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray_micro_bench.php
66 lines (56 loc) · 1.32 KB
/
array_micro_bench.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
65
66
<?php
function array_merge_loop($n) {
$a = range(1, 3);
for ($i = 0; $i < $n; ++$i) {
$a = array_merge($a, [1, 2, 3]);
}
}
function array_merge_loop_with_temporary($n) {
$a = [];
for ($i = 0; $i < $n; ++$i) {
$a = array_merge($a, [1, 2]) + [100];
}
}
function array_merge_loop_nested($n) {
$a = [];
for ($i = 0; $i < $n; ++$i) {
$a = array_merge($a, [1, 2]) + [$a];
}
}
function array_unique_loop($n) {
$a = range(1, 500);
$a += $a;
for ($i = 0; $i < $n; ++$i) {
$a = array_unique($a);
}
}
function user_func_helper($array) {
for ($i = 0; $i < 100; $i++) {
$array[$i] = $i;
}
return $array;
}
function user_func($n) {
$a = [];
for ($i = 0; $i < $n; ++$i) {
$a = user_func_helper($a);
}
}
/*****/
require 'bench_common.php';
const N = 20000;
$t0 = $t = start_test();
empty_loop(N);
$t = end_test($t, 'empty_loop');
$overhead = $last_time;
array_merge_loop(N);
$t = end_test($t, 'array_merge', $overhead);
array_merge_loop_with_temporary(N);
$t = end_test($t, 'array_merge temp', $overhead);
array_merge_loop_nested(N);
$t = end_test($t, 'array_merge nest', $overhead);
array_unique_loop(N);
$t = end_test($t, 'array_unique', $overhead);
user_func(N);
$t = end_test($t, 'user_func', $overhead);
total($t0, "Total");