PHP code optimization: PHP foreach and for speed comparison test examples

PHPz
Release: 2023-03-07 09:40:01
Original
2913 people have browsed it

Experiments are an important way for me to learn computer science. Computer science is not a simple intellectual game. It is not essentially a science, but a tool to transform the world. Mathematical methods and experimental methods are the basic methods of computer research and the basic methods of our learning. Mathematics exercises our thinking ability, and experiments exercise our operational ability and ability to solve practical problems. Our daily work should be regarded as an experiment, and we should summarize what is useful to us from our daily work. For example, to write PHP code well, a very important thing is efficiency. Whether the efficiency is high or not, we have to do experiments. The following is my evaluation of several array loop processing methods in php. The test code is very simple: Example php foreach and for speed comparison (What is PHP foreach?)

<?php
/**
 * array_walk 和 foreach, for 的效率的比较。
 * 我们要测试的是foreach, for, 和 array_walk的效率的问题。 
 */

//产生一个10000的一个数组。
$max = 10000;
$test_arr = range(0, $max);
$temp;
//我们分别用三种方法测试求这些数加上1的值的时间。

// for 的方法
$t1 = microtime(true);
for ($i = 0; $i < $max; $i++) {
    $temp = $temp + 1;
}
$t2 = microtime(true);
$t = $t2 - $t1;
echo "就使用for, 没有对数组操作 花费: {$t}\n";

$t1 = microtime(true);
for ($i = 0; $i < $max; $i++) {
    $test_arr[$i] = $test_arr[$i] + 1;
}
$t2 = microtime(true);
$t = $t2 - $t1;
echo "使用for 并且直接对数组进行了操作 花费: {$t}\n";

$t1 = microtime(true);
for ($i = 0; $i < $max; $i++) {
    addOne($test_arr[$i]);
}
$t2 = microtime(true);
$t = $t2 - $t1;
echo "使用for 调用函数对数组操作 花费 : {$t}\n";

$t1 = microtime(true);
foreach ($test_arr as $k => &$v) {
    $temp = $temp + 1;
}
$t2 = microtime(true);
$t = $t2 - $t1;
echo "使用 foreach 没有对数组操作 花费 : {$t}\n";

$t1 = microtime(true);
foreach ($test_arr as $k => &$v) {
    $v = $v + 1;
}
$t2 = microtime(true);
$t = $t2 - $t1;
echo "使用 foreach 直接对数组操作 : {$t}\n";

$t1 = microtime(true);
foreach ($test_arr as $k => &$v) {
    addOne($v);
}
$t2 = microtime(true);
$t = $t2 - $t1;
echo "使用 foreach 调用函数对数组操作 : {$t}\n";

$t1 = microtime(true);
array_walk($test_arr, &#39;addOne&#39;);
$t2 = microtime(true);
$t = $t2 - $t1;
echo "使用 array_walk 花费 : {$t}\n";

function addOne(&$item) {
    $item = $item + 1;
}
?>
Copy after login


Result of execution:
Just use for, no cost for array operations: 0.15388584136963
Use foreach, no cost for array operations: 0.076934814453125

Use for and directly operate the array cost: 0.14769005775452
Use foreach to directly operate the array: 0.076115131378174


Use for call The cost of the function's array operation: 0.32393312454224
Use foreach to call the function to operate the array: 0.25716996192932
The cost of using array_walk: 0.17966890335083


In the process of operating 10,000 numbers, this We can draw this conclusion from the experiment:

foreach is much more efficient than for. Perhaps a big reason is that for needs to perform many conditional judgments. So in the future, use foreach wherever you can use it, which can double the efficiency.

If you want to call a function within a loop, it is best to use array_walk, which is twice as efficient as for and 43% more efficient than foreach.

Another tip is that if your program has very high efficiency requirements, don’t call functions in deep loops. Use array_walk to call functions. It’s best to write the code directly. inside the loop.

The above is the detailed content of PHP code optimization: PHP foreach and for speed comparison test examples. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template