-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloop_speed_text.php
99 lines (71 loc) · 1.67 KB
/
loop_speed_text.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/php
<?php
ini_set('memory_limit', '500M');
$max = 1000000;
$data = range(0, $max);
echo "\n\rSTART\n\r\n\r";
echo "STATEMENT/FUNC\t\t| TIME(secs)\n\r";
// array_filter loop
$time_start = microtime(true);
$newData = array_filter($data, function ($item) {
return $item;
});
echo sprintf(
"* %-15s\t| %.12f\n\r",
"array_filter", microtime(true) - $time_start);
// array_map loop
$time_start = microtime(true);
$newData = array_map(function ($item) {
return $item;
}, $data);
echo sprintf(
"* %-15s\t| %.12f\n\r",
"array_map", microtime(true) - $time_start);
// array_walk loop
$time_start = microtime(true);
$newData = array_walk($data, function ($item) {
return $item;
});
echo sprintf(
"* %-15s\t| %.12f\n\r",
"array_walk", microtime(true) - $time_start);
// Foreach loop
$newData = [];
$time_start = microtime(true);
foreach ($data as $item) {
$newData[] = $item;
}
echo sprintf(
"* %-15s\t| %.12f\n\r",
"foreach", microtime(true) - $time_start);
// Foreach loop (by &refrence)
$newData = [];
$time_start = microtime(true);
foreach ($data as &$item) {
$item = $item;
}
echo sprintf(
"* %-15s\t| %.12f\n\r",
"foreach(&ref)", microtime(true) - $time_start);
// For loop
$newData = [];
$time_start = microtime(true);
for($i=0;$i < $max;$i++) {
$newData[] = $data[$i];
}
echo sprintf(
"* %-15s\t| %.12f\n\r",
"for", microtime(true) - $time_start);
// While loop
$newData = [];
$time_start = microtime(true);
$i = 0;
while ($i < $max) {
$newData[] = $data[$i];
$i++;
}
echo sprintf(
"* %-15s\t| %.12f\n\r",
"while", microtime(true) - $time_start);
echo "\n\r\n\rEND\n\r";
?>