3

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\n", $upperChrElapsed);
printf("upperRangeChrElapsed: %f\n", $upperRangeChrElapsed);
printf("upperRangeAZElapsed: %f\n", $upperRangeAZElapsed);

?>
Jacob S
  • 53
  • 6
  • I don't know about you, but i've never had the extra 2 microseconds worry me in the least. This is my main problem with PHP: not the language itself, but the ricers who insist on doing benchmarks of useless crap. A million iterations of some stuff that shouldn't be in a loop anyway takes an extra 2 seconds! Oh gn0! pfft – cHao Feb 18 '12 at 01:33
  • 1
    @cHao Speak for yourself. An additional 2 microseconds in the main loop of our app costs us an additional $3,000 of hardware we have to buy per datacenter. If it's 2 microseconds in an inner loop, it costs more. – Crashworks Feb 18 '12 at 01:52
  • @Crashworks: If that little speed difference costs that much money, then frankly, you shouldn't be using PHP. You could use C++ (or even (C#)) instead and it'd save $millions. Of course, the real optimization here (moving the create-the-same-array-we-just-used stuff out of the loop) is free. Point is, the speed difference between the fastest two is negligible, and the speed issue can be better fixed by improving the algorithm than by creating the array faster. – cHao Feb 18 '12 at 06:37

1 Answers1

2

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

Community
  • 1
  • 1
Louis-Philippe Huberdeau
  • 5,341
  • 1
  • 19
  • 22