Home php教程 php手册 foreach、while、for性能比较

foreach、while、for性能比较

Jun 13, 2016 am 10:34 AM
for foreach while generally performance case array method Compare Traverse

一般情况下,遍历一个数组有三种方法,for、while、foreach。其中最简单方便的是foreach。那么它们在操作和性能上存在什么差别,通常使用那种方法比较好。
下面先让我们来测试一下共同遍历一个有50000个下标的一维数组所耗的时间:

测试平台:
CPU:P-M 725
内存:512M
硬盘:40G 5400转
OS:Windows XP SP2
WEB:apache 2.0.54  php5.0.4

测试代码:
/*
  * @ Author: Lilov
  * @ Homepage: www.codesky.com
  * @ E-mail: zhongjiechao@gmail.com
  *
  */

$arr = array();
for($i = 0; $i $arr[] = $i*rand(1000,9999);
}

function GetRunTime()
{
list($usec,$sec)=explode(" ",microtime());
return ((float)$usec+(float)$sec);
}
######################################
$time_start = GetRunTime();

for($i = 0; $i $str .= $arr[$i];
}

$time_end = GetRunTime();
$time_used = $time_end - $time_start;

echo Used time of for:.round($time_used, 7).(s)

;
unset($str, $time_start, $time_end, $time_used);
######################################
$time_start = GetRunTime();

while(list($key, $val) = each($arr)){
$str .= $val;
}

$time_end = GetRunTime();
$time_used = $time_end - $time_start;

echo Used time of while:.round($time_used, 7).(s)

;
unset($str, $key, $val, $time_start, $time_end, $time_used);
######################################
$time_start = GetRunTime();

foreach($arr as $key => $val){
$str .= $val;
}

$time_end = GetRunTime();
$time_used = $time_end - $time_start;
echo Used time of foreach:.round($time_used, 7).(s)

;
######################################

?>

测试结果:

将三次测试结果求平均值:
分别对应for、while、foreach
0.1311650
0.1666853
0.1237440

经过反复多次测试,结果表明,对于遍历同样一个数组,foreach速度最快,最慢的则是while。foreach比while大约快20% ~ 30%左右。随后再把数组下标增加到500000、5000000测试结果也一样。但从原理上来看,foreach是对数组副本进行操作(通过拷贝数组),而while则通过移动数组内部指标进行操作,一般逻辑下认为,while应该比foreach快(因为foreach在开始执行的时候首先把数组复制进去,而while直接移动内部指标。),但结果刚刚相反。原因应该是,foreach是PHP内部实现,而while是通用的循环结构。

所以,在通常应用中我更喜欢用foreach形式,简单,而且效率高。在PHP5下,  foreach还可以遍历类的属性。

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 Article Tags

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)

The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs) The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs) May 04, 2024 pm 06:01 PM

The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs)

How to set font size on mobile phone (easily adjust font size on mobile phone) How to set font size on mobile phone (easily adjust font size on mobile phone) May 07, 2024 pm 03:34 PM

How to set font size on mobile phone (easily adjust font size on mobile phone)

How to choose a mobile phone screen protector to protect your mobile phone screen (several key points and tips for purchasing mobile phone screen protectors) How to choose a mobile phone screen protector to protect your mobile phone screen (several key points and tips for purchasing mobile phone screen protectors) May 07, 2024 pm 05:55 PM

How to choose a mobile phone screen protector to protect your mobile phone screen (several key points and tips for purchasing mobile phone screen protectors)

Performance comparison of different Java frameworks Performance comparison of different Java frameworks Jun 05, 2024 pm 07:14 PM

Performance comparison of different Java frameworks

PHP array key value flipping: Comparative performance analysis of different methods PHP array key value flipping: Comparative performance analysis of different methods May 03, 2024 pm 09:03 PM

PHP array key value flipping: Comparative performance analysis of different methods

How to optimize the performance of multi-threaded programs in C++? How to optimize the performance of multi-threaded programs in C++? Jun 05, 2024 pm 02:04 PM

How to optimize the performance of multi-threaded programs in C++?

Application of PHP array grouping function in data sorting Application of PHP array grouping function in data sorting May 04, 2024 pm 01:03 PM

Application of PHP array grouping function in data sorting

The role of PHP array grouping function in finding duplicate elements The role of PHP array grouping function in finding duplicate elements May 05, 2024 am 09:21 AM

The role of PHP array grouping function in finding duplicate elements

See all articles