开发者

why is PHP strtolower performance so varying?

I have done some profiling on a site and found that strtolower calls took unexpectedly long time.

The context is


function __autoload($class_name) {  
  require_once('app/model/' . strtolower($class_name) . '.php');  
}

And the result is

_0.0092 -> ___autoload() C:\xxx\config.php:0_

0.0093 -> strtolower() C:\xxx\config.php:77

0.0101 -> require-once(C:\xxx.php) C:\xxx\config.php:77

I've seen this on several places in the trace file.

I then tried the function in the following context


for($i=0;$i<100;$i++) {  
  strtolower('SomeStRIng' . $i)  
}

And the result was

0.0026 -> strtolower() C:\xxx\index.php:53

0.0027 -> strtolower() C:\xxx\index.php:53

0.0027 -> strtolower() C:\xxx\index.php:53

0.0027 -> strtolower() C:\xxx\index.php:53

There is a notable difference between the two. It's no biggie overall of course but I'm still con开发者_Go百科fused.


You're running far too small tests, on far too little data. You'll never get consistent data, as other system factors (like CPU speed/load) will take a far greater toll.

Your first test is disk-bound. Lowering the case of a (hopefully reasonably) short string is essentially instantaneous, or at least measured in microseconds. Hitting the disk to locate/load/parse a file will take on the order of milliseconds. You're trying to detect a difference in something where the part you're not concerned about takes 1000 times longer. ie: the strtolower overhead is a rounding error.

Your second test, while being purely cpu/memory bound, is also too short to be practical. You can't be sure that doing 100 string concatenations (and associated memory allocation) won't overwhelm the actual lowercasing. A better test would be to prebuild a series of mix-case strings (a few hundred or thousand of them), then loop over that array repeatedly and strtolower in seuqnce. That way you've eliminated as much overhead/irrelevant code path as possible, and should hopefully get more consistent data.


Which profiler did you use? XDebug?

I would suspect it's a problem with the profiler, as you are showing quite a significant difference. See if this profiles any differently...

function __autoload($class_name) {  
  $file=strtolower($class_name);
  require_once('app/model/' . $file . '.php');  
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜