douzuita7325 2012-02-18 01:18
浏览 67
已采纳

PHP的范围('A','Z')是否返回静态数组?

I was overlooking some code that I had written to generate an A-Z navigation on a product page, and the method in which it was done was a for loop; using ascii octals 65-91 and PHP's chr() function. I wondered if there was a simpler and/or more efficient way of doing this, and I discovered that PHP's range() function supports alphabetical ranges.

After I wrote my test code to compare the different methods, a few questions came to mind:

  1. Does PHP store a static array of the alphabet?
  2. How can I profile more deeply to look below the PHP layer to see what's happening?

I have a cachegrind of the PHP script that can be attached if necessary, in addition to environment config. For those who might want to know the machine specs in which it was executed, here are some links:

root@workbox:~$ lshw http://pastebin.com/cZZRjJcR

root@workbox:~$ sysinfo http://pastebin.com/ihQkkPAJ

<?php
/*
 * determine which method out of 3 for returning
 * an array of uppercase alphabetic characters 
 * has the highest performance
 * 
 * +++++++++++++++++++++++++++++++++++++++++++++
 * 
 * 1) Array $alpha = for($x = 65; $x < 91; $x++) { $upperChr[] = chr($x); }
 * 2) Array $alpha = range(chr(65), chr(90);
 * 3) Array $alpha = range('A', 'Z');
 * 
 * +++++++++++++++++++++++++++++++++++++++++++++
 * 
 * test runs with iterations:
 * 
 * 10,000:
 * - 1) upperChrElapsed: 0.453785s
 * - 2) upperRangeChrElapsed: 0.069262s
 * - 3) upperRangeAZElapsed: 0.046110s
 * 
 * 100,000:
 * - 1) upperChrElapsed: 0.729015s
 * - 2) upperRangeChrElapsed: 0.078652s
 * - 3) upperRangeAZElapsed: 0.052071s
 * 
 * 1,000,000:
 * - 1) upperChrElapsed: 50.942950s
 * - 2) upperRangeChrElapsed: 10.091785s
 * - 3) upperRangeAZElapsed: 8.073058s
 */

ini_set('max_execution_time', 0);
ini_set('memory_limit', 0);

define('ITERATIONS', 1000000); // 1m loops x3

$upperChrStart = microtime(true);
for($i = 0; $i <= ITERATIONS; $i++) {
    $upperChr = array();
    for($x = 65; $x < 91; $x++) {
            $upperChr[] = chr($x);
    }
}
$upperChrElapsed = microtime(true) - $upperChrStart;

// +++++++++++++++++++++++++++++++++++++++++++++

$upperRangeChrStart = microtime(true);
for($i = 0; $i <= ITERATIONS; $i++) {
    $upperRangeChr = range(chr(65), chr(90));   
}
$upperRangeChrElapsed = microtime(true) - $upperRangeChrStart;

// +++++++++++++++++++++++++++++++++++++++++++++

$upperRangeAZStart = microtime(true);
for($i = 0; $i <= ITERATIONS; $i++) {
    $upperRangeAZ = range('A', 'Z');    
}
$upperRangeAZElapsed = microtime(true) - $upperRangeAZStart;

printf("upperChrElapsed: %f
", $upperChrElapsed);
printf("upperRangeChrElapsed: %f
", $upperRangeChrElapsed);
printf("upperRangeAZElapsed: %f
", $upperRangeAZElapsed);

?>
  • 写回答

1条回答 默认 最新

  • dongyong9224 2012-02-18 01:28
    关注

    Does PHP waste memory keeping an array of letters? I would doubt it. range() will work on a wide variety of values too.

    If performance is an issue in such a case, you might want to declare the array outside of the loop so that it can be re-used. However, large gains rarely come from micro-optimizations. Using profiling on larger applications to get significant gains.

    As for profiling at a lower level, you can simply use valgrind on PHP CLI. I've also seen it used on an apache process.

    Related: How to profile my C++ application on linux

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路