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

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

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵