Skip to content

Commit 4749e5b

Browse files
committed
Add array_micro_bench.php
1 parent ddbdd59 commit 4749e5b

File tree

4 files changed

+116
-81
lines changed

4 files changed

+116
-81
lines changed

Zend/array_micro_bench.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
function array_merge_loop($n) {
4+
$a = range(1, 3);
5+
for ($i = 0; $i < $n; ++$i) {
6+
$a = array_merge($a, [1, 2, 3]);
7+
}
8+
}
9+
10+
function array_merge_loop_with_temporary($n) {
11+
$a = [];
12+
for ($i = 0; $i < $n; ++$i) {
13+
$a = array_merge($a, [1, 2]) + [100];
14+
}
15+
}
16+
17+
function array_merge_loop_nested($n) {
18+
$a = [];
19+
for ($i = 0; $i < $n; ++$i) {
20+
$a = array_merge($a, [1, 2]) + [$a];
21+
}
22+
}
23+
24+
function array_unique_loop($n) {
25+
$a = range(1, 500);
26+
$a += $a;
27+
for ($i = 0; $i < $n; ++$i) {
28+
$a = array_unique($a);
29+
}
30+
}
31+
32+
function user_func_helper($array) {
33+
for ($i = 0; $i < 100; $i++) {
34+
$array[$i] = $i;
35+
}
36+
return $array;
37+
}
38+
39+
function user_func($n) {
40+
$a = [];
41+
for ($i = 0; $i < $n; ++$i) {
42+
$a = user_func_helper($a);
43+
}
44+
}
45+
46+
/*****/
47+
48+
require 'bench_common.php';
49+
50+
const N = 20000;
51+
52+
$t0 = $t = start_test();
53+
empty_loop(N);
54+
$t = end_test($t, 'empty_loop');
55+
$overhead = $last_time;
56+
array_merge_loop(N);
57+
$t = end_test($t, 'array_merge', $overhead);
58+
array_merge_loop_with_temporary(N);
59+
$t = end_test($t, 'array_merge temp', $overhead);
60+
array_merge_loop_nested(N);
61+
$t = end_test($t, 'array_merge nest', $overhead);
62+
array_unique_loop(N);
63+
$t = end_test($t, 'array_unique', $overhead);
64+
user_func(N);
65+
$t = end_test($t, 'user_func', $overhead);
66+
total($t0, "Total");

Zend/bench.php

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -345,41 +345,7 @@ function strcat($n) {
345345

346346
/*****/
347347

348-
function gethrtime()
349-
{
350-
$hrtime = hrtime();
351-
return (($hrtime[0]*1000000000 + $hrtime[1]) / 1000000000);
352-
}
353-
354-
function start_test()
355-
{
356-
ob_start();
357-
return gethrtime();
358-
}
359-
360-
function end_test($start, $name)
361-
{
362-
global $total;
363-
$end = gethrtime();
364-
ob_end_clean();
365-
$total += $end-$start;
366-
$num = number_format($end-$start,3);
367-
$pad = str_repeat(" ", 24-strlen($name)-strlen($num));
368-
369-
echo $name.$pad.$num."\n";
370-
ob_start();
371-
return gethrtime();
372-
}
373-
374-
function total()
375-
{
376-
global $total;
377-
$pad = str_repeat("-", 24);
378-
echo $pad."\n";
379-
$num = number_format($total,3);
380-
$pad = str_repeat(" ", 24-strlen("Total")-strlen($num));
381-
echo "Total".$pad.$num."\n";
382-
}
348+
require 'bench_common.php';
383349

384350
$t0 = $t = start_test();
385351
simple();

Zend/bench_common.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
function empty_loop($n) {
4+
for ($i = 0; $i < $n; ++$i) {
5+
}
6+
}
7+
8+
function gethrtime()
9+
{
10+
$hrtime = hrtime();
11+
return (($hrtime[0]*1000000000 + $hrtime[1]) / 1000000000);
12+
}
13+
14+
function start_test()
15+
{
16+
ob_start();
17+
return gethrtime();
18+
}
19+
20+
function end_test($start, $name, $overhead = null)
21+
{
22+
global $total;
23+
global $last_time;
24+
$end = gethrtime();
25+
ob_end_clean();
26+
$last_time = $end-$start;
27+
$total += $last_time;
28+
$num = number_format($last_time,3);
29+
$pad = str_repeat(" ", 24-strlen($name)-strlen($num));
30+
if (is_null($overhead)) {
31+
echo $name.$pad.$num."\n";
32+
} else {
33+
$num2 = number_format($last_time - $overhead,3);
34+
echo $name.$pad.$num." ".$num2."\n";
35+
}
36+
ob_start();
37+
return gethrtime();
38+
}
39+
40+
function total()
41+
{
42+
global $total;
43+
$pad = str_repeat("-", 24);
44+
echo $pad."\n";
45+
$num = number_format($total,3);
46+
$pad = str_repeat(" ", 24-strlen("Total")-strlen($num));
47+
echo "Total".$pad.$num."\n";
48+
}

Zend/micro_bench.php

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -233,52 +233,7 @@ function ternary2($n) {
233233

234234
/*****/
235235

236-
function empty_loop($n) {
237-
for ($i = 0; $i < $n; ++$i) {
238-
}
239-
}
240-
241-
function gethrtime()
242-
{
243-
$hrtime = hrtime();
244-
return (($hrtime[0]*1000000000 + $hrtime[1]) / 1000000000);
245-
}
246-
247-
function start_test()
248-
{
249-
ob_start();
250-
return gethrtime();
251-
}
252-
253-
function end_test($start, $name, $overhead = null)
254-
{
255-
global $total;
256-
global $last_time;
257-
$end = gethrtime();
258-
ob_end_clean();
259-
$last_time = $end-$start;
260-
$total += $last_time;
261-
$num = number_format($last_time,3);
262-
$pad = str_repeat(" ", 24-strlen($name)-strlen($num));
263-
if (is_null($overhead)) {
264-
echo $name.$pad.$num."\n";
265-
} else {
266-
$num2 = number_format($last_time - $overhead,3);
267-
echo $name.$pad.$num." ".$num2."\n";
268-
}
269-
ob_start();
270-
return gethrtime();
271-
}
272-
273-
function total()
274-
{
275-
global $total;
276-
$pad = str_repeat("-", 24);
277-
echo $pad."\n";
278-
$num = number_format($total,3);
279-
$pad = str_repeat(" ", 24-strlen("Total")-strlen($num));
280-
echo "Total".$pad.$num."\n";
281-
}
236+
require 'bench_common.php';
282237

283238
const N = 5000000;
284239

0 commit comments

Comments
 (0)