-
Notifications
You must be signed in to change notification settings - Fork 45
/
test_php_function.php
59 lines (50 loc) · 1.38 KB
/
test_php_function.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
<?php
/**
* This small script will tell you which of the PHP functions how much they eat resources and spend time.
* It will help not to use super productive functions.
* I advise you to test on localhost.
*/
$total = 5000;
// Craft array
$array = array();
for ($j = 0; $j < $total; $j++) {
$array[$j] = 'a' . $j;
}
// check "in_array"
$s = microtime(true);
for ($j = 0; $j < $total; $j++) {
in_array("a555", $array);
}
echo "in_array: " . (microtime(true) - $s) . "\n";
// check "array_flip"
$s = microtime(true);
$array = array_flip($array);
echo "<br />array_flip: " . (microtime(true) - $s) . "\n";
// check "array_key_exists"
$s = microtime(true);
for ($j = 0; $j < $total; $j++) {
array_key_exists("555", $array);
}
echo "<br />array_key_exists: " . (microtime(true) - $s) . "\n";
// check "isset"
$s = microtime(true);
for ($j = 0; $j < $total; $j++) {
if(isset($array[555])) $c = true;
}
echo "<br />isset: " . (microtime(true) - $s) . "\n";
// check "isset" with "foreach"
$s = microtime(true);
for ($j = 0; $j < $total; $j++) {
foreach($array AS $k=>$v) {
if(isset($v) && $v == "a555") $c = true;
}
}
echo "<br />foreach isset: " . (microtime(true) - $s) . "\n";
// check "foreach"
$s = microtime(true);
for ($j = 0; $j < $total; $j++) {
foreach($array AS $k=>$v) {
if($v == "a555") $c = true;
}
}
echo "<br />foreach: " . (microtime(true) - $s) . "\n";