Table of Contents
在对10000个数的操作过程中,这个实验我们可以得出这样的结论:
Home Backend Development PHP Tutorial PHP中array_walk 和 foreach, for 的效率比较及性能优化

PHP中array_walk 和 foreach, for 的效率比较及性能优化

Jun 20, 2016 pm 01:02 PM

实践是我学习计算机科学的一个重要方法,计算机科学不是简单的智力游戏,它本质上来说不是一门科学,而是一个改造世界的工具。

数学方法和实验方法是计算机研究的基本方法,也是我们学习的基本方法,数学锻炼我们的思维能力,实验锻炼我们的操作能力,解决实际问题的能力。

我们每天的工作都应该看做是一次实验,要从每天的工作中,总结出对我们来说有用的东西。

比如要写好php代码,一个很重要的东西就是效率,效率高不高,我们就要做实验。下面是我对php中的几个数组循环处理方法的评测,测试的代码很简单:

<p><?php</p>/**<br />* array_walk 和 foreach, for 的效率的比较。<br />* 我们要测试的是foreach, for, 和 array_walk的效率的问题。 <br />*/<br /><br />//产生一个10000的一个数组。<br />$max = 10000;<br />$test_arr = range(0, $max);<br />$temp;<br />//我们分别用三种方法测试求这些数加上1的值的时间。<br /><br />// for 的方法<br />$t1 = microtime(true);<br />for ($i = 0; $i < $max; $i++) {<br />$temp = $temp + 1;<br />}<br />$t2 = microtime(true);<br />$t = $t2 - $t1;<br />echo "就使用for, 没有对数组操作 花费: {$t}\n";<br /><br />$t1 = microtime(true);<br />for ($i = 0; $i < $max; $i++) {<br />$test_arr[$i] = $test_arr[$i] + 1;<br />}<br />$t2 = microtime(true);<br />$t = $t2 - $t1;<br />echo "使用for 并且直接对数组进行了操作 花费: {$t}\n";<br /><br />$t1 = microtime(true);<br />for ($i = 0; $i < $max; $i++) {<br />addOne($test_arr[$i]);<br />}<br />$t2 = microtime(true);<br />$t = $t2 - $t1;<br />echo "使用for 调用函数对数组操作 花费 : {$t}\n";<br /><br />$t1 = microtime(true);<br />foreach ($test_arr as $k => &$v) {<br />$temp = $temp + 1;<br />}<br />$t2 = microtime(true);<br />$t = $t2 - $t1;<br />echo "使用 foreach 没有对数组操作 花费 : {$t}\n";<br /><br />$t1 = microtime(true);<br />foreach ($test_arr as $k => &$v) {<br />$v = $v + 1;<br />}<br />$t2 = microtime(true);<br />$t = $t2 - $t1;<br />echo "使用 foreach 直接对数组操作 : {$t}\n";<br /><br />$t1 = microtime(true);<br />foreach ($test_arr as $k => &$v) {<br />addOne($v);<br />}<br />$t2 = microtime(true);<br />$t = $t2 - $t1;<br />echo "使用 foreach 调用函数对数组操作 : {$t}\n";<br /><br />$t1 = microtime(true);<br />array_walk($test_arr, 'addOne');<br />$t2 = microtime(true);<br />$t = $t2 - $t1;<br />echo "使用 array_walk 花费 : {$t}\n";<br /><br />function addOne(&$item) {<br />$item = $item + 1;<br />}<br /><p>?></p>
Copy after login

执行的结果:

 
就使用for, 没有对数组操作 花费: 0.15388584136963
使用 foreach 没有对数组操作 花费 : 0.076934814453125
 
使用for 并且直接对数组进行了操作 花费: 0.14769005775452
使用 foreach 直接对数组操作 : 0.076115131378174
 
使用for 调用函数对数组操作 花费 : 0.32393312454224
使用 foreach 调用函数对数组操作 : 0.25716996192932
使用 array_walk 花费 : 0.17966890335083
 

在对10000个数的操作过程中,这个实验我们可以得出这样的结论:

 
foreach 的效率要比for 高很多,也许有很大的一个原因是for 要进行很多次条件判断。所以以后能用foreach的地方就用foreach,可以提高1倍的效率。
 
如果循环内要调用函数,用array_walk  最好,它的效率要比for 高出1倍,要比foreach高出43%的效率。还有一个提示就是如果你这个程序对效率的要求是很高的,那不要在很深的循环中调用函数,要调用函数也要用array_walk,最好的直接把代码写在循环里面。


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

See all articles