PHP性能测试Part 1 - Counting Loops(计数循环) - count()
2012-07-19 23:04
302 查看
导读:
以下几篇文字,参考国外网站http://www.phpbench.com/ ,感谢Chris
Vincent 的工作。我所做的,就是重新实践,不过,也加入一些自己的思考,并明确测试环境。并且,会将更新后的脚本放置在SAE,具体地址后面会附上。
本地测试环境:
php 5.4.3
mysql 5.5.24
Apache 2.2.22
CPU T6670
Memory 2GB
OS Win7 32bit
说明
1 脚本采用浏览器短输出而非cli方式,所以,使用了<br />;
2 SAE PHP版本并非5.4,所以,不支持新特性$_SERVER['REQUEST_TIME_FLOAT']。
线上DEMO
依赖SAE。
详细
PART 1 Counting Loops(计数循环)
原测试脚本
A
B
更新后脚本
测试结果
本地
做for循环,步进上限为数组长度 10000 时,
提前 count() 及没有提前 count() 的区别:
提前计算好大小: 0.0055069923400879 秒
没有提前计算好: 11.517592191696 秒
脚本总计耗时为: 11.528949975967 秒
SAE(点击访问)
做for循环,步进上限为数组长度 10000 时,
提前 count() 及没有提前 count() 的区别:
提前计算好大小: 0.00195813179016 秒
没有提前计算好: 8.76361608505 秒
结论
使用提前计算for循环步进上限,使之成为常量,而非计算式。( 服务器就是比笔记本好啊)
以下几篇文字,参考国外网站http://www.phpbench.com/ ,感谢Chris
Vincent 的工作。我所做的,就是重新实践,不过,也加入一些自己的思考,并明确测试环境。并且,会将更新后的脚本放置在SAE,具体地址后面会附上。
本地测试环境:
php 5.4.3
mysql 5.5.24
Apache 2.2.22
CPU T6670
Memory 2GB
OS Win7 32bit
说明
1 脚本采用浏览器短输出而非cli方式,所以,使用了<br />;
2 SAE PHP版本并非5.4,所以,不支持新特性$_SERVER['REQUEST_TIME_FLOAT']。
线上DEMO
依赖SAE。
详细
PART 1 Counting Loops(计数循环)
原测试脚本
A
<?php // Initial Configuration global $x; $i = 0; $tmp = ''; while($i < 10000) { $tmp .= 'a'; ++$i; } $x = array_fill(5, 1000, $tmp); unset($i, $tmp); // Test Source function Test3_1() { global $x; /* The Test */ $t = microtime(true); $size = count($x); for ($i=0; $i<$size; $i++); return (microtime(true) - $t); } // Variable Clean-up function Test3_End() { global $x; unset($x); }
B
<?php // Initial Configuration global $x; $i = 0; $tmp = ''; while($i < 10000) { $tmp .= 'a'; ++$i; } $x = array_fill(5, 1000, $tmp); unset($i, $tmp); // Test Source function Test3_2() { global $x; /* The Test */ $t = microtime(true); for ($i=0; $i<count($x); $i++); return (microtime(true) - $t); } // Variable Clean-up function Test3_End() { global $x; unset($x); }
更新后脚本
<?php echo '做for循环,步进上限为数组长度 10000 时,<br />提前 count() 及没有提前 count() 的区别:<br /><br />'; $x = array_fill(0, 10000, 'a'); // 填充数组,数组长度为 10000, 值大小为 9.5367431640625MB // 提前计算好大小 function with_pre_calc() { global $x; $lst = microtime(true); // 循环开始时间 loop start time 浮点型 $size = count($x); for ($i=0; $i<$size; $i++); return microtime(true) - $lst; } // 没有提前计算好大小 function without_pre_calc() { global $x; $lst = microtime(true); // 循环开始时间 loop start time 浮点型 for ($i = 0; $i < count($x); $i++); return microtime(true) - $lst; } echo '提前计算好大小: ' . with_pre_calc() . ' 秒' . '<br />' . '没有提前计算好: ' . without_pre_calc() . ' 秒' . '<br />'; unset($x); echo '脚本总计耗时为: ' . (microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']) . ' 秒';
测试结果
本地
做for循环,步进上限为数组长度 10000 时,
提前 count() 及没有提前 count() 的区别:
提前计算好大小: 0.0055069923400879 秒
没有提前计算好: 11.517592191696 秒
脚本总计耗时为: 11.528949975967 秒
SAE(点击访问)
做for循环,步进上限为数组长度 10000 时,
提前 count() 及没有提前 count() 的区别:
提前计算好大小: 0.00195813179016 秒
没有提前计算好: 8.76361608505 秒
结论
使用提前计算for循环步进上限,使之成为常量,而非计算式。( 服务器就是比笔记本好啊)
相关文章推荐
- PHP性能测试Part 1 - Counting Loops(计数循环) - sizeof()
- [Php-Mysql]多条数据的循环插入和一次性插入的性能测试
- 单线程环境下Java/PHP/Python性能测试(循环输出)
- 在生产环境中使用 php 性能测试工具 xhprof
- xhprof安装以及php程序性能测试
- 在生产环境中使用 php 性能测试工具 xhprof
- PHP中测试in_array、isset、array_key_exists性能
- PCC(Pulse count control)编程之二:循环计数
- 全面测试JAVA各种循环的性能,for轻松胜出
- PHP归档phar性能测试
- testng性能测试,循环测试,线程测试,优先级
- PHP性能测试工具xhprof安装与使用
- php 数组函数性能测试
- PHP使用Apache中的ab测试网站的压力性能
- PHP的性能测试全过程分享
- java -- while循环拆分性能测试程序
- PHP中将字符串转化为整数(int) intval() printf() 性能测试
- php的serialize序列化和json性能测试
- [C++] 测试硬件popcnt(位1计数)指令与各种软件算法,利用模板实现静态多态优化性能
- php性能优化(二)性能测试工具篇