


PHP code optimization: PHP foreach and for speed comparison test examples
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, 'addOne'); $t2 = microtime(true); $t = $t2 - $t1; echo "使用 array_walk 花费 : {$t}\n"; function addOne(&$item) { $item = $item + 1; } ?>
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Kernelsecuritycheckfailure (kernel check failure) is a relatively common type of stop code. However, no matter what the reason is, the blue screen error causes many users to be very distressed. Let this site carefully introduce 17 types to users. Solution. 17 solutions to kernel_security_check_failure blue screen Method 1: Remove all external devices When any external device you are using is incompatible with your version of Windows, the Kernelsecuritycheckfailure blue screen error may occur. To do this, you need to unplug all external devices before trying to restart your computer.

Many friends complain about the slow download speed of Uplay, and the download speed of Uplay platform is too slow. Sometimes it is only a few KB, so how to solve it? It is mainly the problem of dns and hosts files. Below, I will tell you about the slow download speed of Uplay. Let’s take a look at the solutions in detail. Uplay download speed is always 0. Solution 1. Double-click to open this computer! As shown in the picture below 2. Enter this computer and find your system disk. Generally, the default is the c drive, as shown in the picture below 3. Then enter this folder: C:\Windows\System32\drivers\etc\, as shown below As shown in the figure below 4. Right-click the hosts file - select Open method, as shown in the figure below 5. Open

How to solve the slow upload speed of Win10 computer? When we use the computer, we may feel that the file upload speed of our computer is very slow. So what is the situation? In fact, this is because the default upload speed of the computer is 20%, so the upload speed is very slow. Many friends do not know how to operate in detail. The editor has compiled the steps to format the C drive in Win11 below. If you are interested, follow Let’s take a look below! Solution to the slow upload speed of Win10 1. Press win+R to call up run, enter gpedit.msc, and press Enter. 2. Select the management template, click Network--Qos Packet Scheduler, and double-click Limit to reserve bandwidth. 3. Select Enabled, which will bring

Will formatting a laptop make it faster? If you want to format your Windows laptop but want to know if it will make it faster, this article will help you know the right answer to this question. Will formatting a laptop make it faster? There are many reasons why users format their Windows laptops. But the most common reason is slow performance or speed of your laptop. Formatting a laptop will completely delete all data stored on the C drive or the hard drive partition where Windows operating system is installed. Therefore, every user will think twice before taking this step, especially when it comes to the performance of the laptop. This article will help you understand whether formatting your laptop will speed it up. Formatting your laptop helps

Laravel is a popular PHP development framework, but it is sometimes criticized for being as slow as a snail. What exactly causes Laravel's unsatisfactory speed? This article will provide an in-depth explanation of the reasons why Laravel is as slow as a snail from multiple aspects, and combine it with specific code examples to help readers gain a deeper understanding of this problem. 1. ORM query performance issues In Laravel, ORM (Object Relational Mapping) is a very powerful feature that allows

Can Win10 skype be uninstalled? This is a question that many users want to know, because many users find that this application is included in the default program on their computers, and they are worried that deleting it will affect the operation of the system. Let this website help users Let’s take a closer look at how to uninstall Skype for Business in Win10. How to uninstall Skype for Business in Win10 1. Click the Windows icon on the computer desktop, and then click the settings icon to enter. 2. Click "Apply". 3. Enter "Skype" in the search box and click to select the found result. 4. Click "Uninstall". 5

Is single-core Google Chrome slower than dual-core browsers? Nowadays, many people are using various browsers to surf the Internet, and Google Chrome is one of them. As the leader in browsers, its technology is unquestionable. Some friends often ask the editor, what do you mean by single- and dual-core browsers? Will this affect browser loading speed? Today I will talk to you about this issue. Single-core and dual-core browser speed PK of Google Chrome First of all, the editor will give you a conclusion: the single-core Google Chrome is not slower than the dual-core browser. The core of the browser is not like a battery, the more there are, the stronger it is. One of the cores of the dual-core browser is the IE browser core, and the other core is WebKit.

How to improve the speed of large-scale data sorting through PHP multi-threading. With the rapid development of the Internet and the popularity of big data, the demand for processing massive data is also increasing. Among them, for the common problem of data sorting, how to improve the processing speed has become an urgent problem to be solved. In the field of PHP, multi-threading technology is considered an effective solution. This article will introduce how to improve the speed of large-scale data sorting through PHP multi-threading. 1. The principle of multi-threading Multi-threading refers to the existence of multiple threads at the same time. Multiple threads can execute different tasks at the same time.
