-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbenchmark_arraymap_foreach.php
53 lines (44 loc) · 1.29 KB
/
benchmark_arraymap_foreach.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
<?php
$numbers = range(0, 1000);
include "Collection.php";
$instances=5000;
// **********************************************************************************
$t1=microtime(true);
for($i=0;$i<$instances;$i++) {
$result = array();
foreach ($numbers as $number) {
$result[] = $number * 10;
}
}
$t2=microtime(true);
$table['foreach']=$t2-$t1;
// **********************************************************************************
$t1=microtime(true);
for($i=0;$i<$instances;$i++) {
$result = array_map(function ($number) {
return $number * 10;
}, $numbers);
}
$t2=microtime(true);
$table['array_map']=$t2-$t1;
// **********************************************************************************
$t1=microtime(true);
for($i=0;$i<$instances;$i++) {
$result = array_map(static function ($number) {
return $number * 10;
}, $numbers);
}
$t2=microtime(true);
$table['array_map (static)']=$t2-$t1;
// **********************************************************************************
$t1=microtime(true);
function tenTimes($number)
{
return $number * 10;
}
for($i=0;$i<$instances;$i++) {
$result = array_map('tenTimes', $numbers);
}
$t2=microtime(true);
$table['array_map (calling a function)']=$t2-$t1;
echo \mapache_commons\Collection::generateTable($table);