[development] coding standard question

Steve Rude steve at achieveinternet.com
Mon Aug 27 19:13:45 UTC 2007


I ran this test just to see what would happen, and here are the results, 
feel free to test it yourself.  According to this test, running a simple 
loop with one if statement 100k times, you lose about 20ms using 
uppercase.  What is interesting, is that the same test in php5 runs 
100ms faster. 

slantview:~ steverude$ /Applications/xampp/xamppfiles/bin/php-4.4.7 
test.php
Time for UPPERCASE: 723.675251007 ms

Time for lowercase: 702.354669571 ms

slantview:~ steverude$ /Applications/xampp/xamppfiles/bin/php-5.2.2 
test.php
Time for UPPERCASE: 622.086286545 ms

Time for lowercase: 607.203245163 ms


Another interesting stat is that we have 2383 times in a stock drupal 
5.1 distribution where we use an uppercase TRUE, FALSE or NULL.

Reference:

slantview:~/Documents/workspace/drupal-5.1 steverude$ grep -r TRUE * | wc -l
     629
slantview:~/Documents/workspace/drupal-5.1 steverude$ grep -r FALSE * | 
wc -l
     511
slantview:~/Documents/workspace/drupal-5.1 steverude$ grep -r NULL * | wc -l
    1243

So it does not seem that unreasonable to me that we could potentially 
get some performance gain by standardizing on lower case true, false and 
null.  Preference aside (me being on the uppercase side), I believe that 
adding this standard to the drupal standards could potentially give us a 
small amount of performance increase.

I also think the issue should be documented in the coding standard and 
reflected in the Coder module regardless of the decision.

Here is the code for the test:

slantview:~ steverude$ cat test.php
<?php

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

$var = true;

$time_total = 0;
for ($i = 0; $i < 100000; ++$i) {
  $time_start = microtime_float();
  if ($var === TRUE) {
    $time_end = microtime_float();
    $time_total += $time_end - $time_start;
  }
}
$time_total = $time_total * 1000;
print "Time for UPPERCASE: $time_total ms\n\n";

$time_total = 0;
for ($i = 0; $i < 100000; ++$i) {
  $time_start = microtime_float();
  if ($var === true) {
    $time_end = microtime_float();
    $time_total += $time_end - $time_start;
  }
}
$time_total = $time_total * 1000;
print "Time for lowercase: $time_total ms\n\n";

Thanks,

Steve Rude

Karoly Negyesi wrote:
> I do not believe that true could be faster than TRUE when php is case agnostic about its identifiers. Maybe there is time taken to lowercase TRUE ... but if you can measure lowercasing four characters in the parsing stage where only ASCII is supported, congratulations. 
>
> About commas, yes the D5 menu defintiions are ugly sometimes. Have you seen D6 :) ?
>
>   


-- 
Steve Rude + Lead Developer
*AchieveInternet*
(800) 618-877 x 202

http://www.achieveinternet.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.drupal.org/pipermail/development/attachments/20070827/9e9e597b/attachment.htm 


More information about the development mailing list