Why does PHP strtolower's performance vary so much?

I did some profiling on the site and found that strtolower calls took unexpectedly long time.

Context


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 saw this in several places in the trace file.

Then I 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 noticeable difference between them. This, of course, is not big, but I'm still confused.

+4
source share
2 answers

You have too small tests, on too small data. You will never get consistent data, as other system factors (such as processor speed / load) will be much larger.

Your first test is disk related. The omission of a case (hopefully reasonable) of a short string is essentially instantaneous, or at least measured in microseconds. Finding a drive to search / download / parse a file will take about milliseconds. You are trying to detect a difference in something where the part you are not disturbing takes 1000 times longer. i.e.: strtolower overhead - rounding error.

Your second test, being purely cpu / memory bound, is also too short to be practical. You cannot be sure that performing 100 string concatenations (and the corresponding memory allocation) will not suppress the actual subscript. The best test would be to preinstall a series of lines with impurities (several hundred or thousands of them), and then cycle through this array and strtolower in seuqnce. This way you eliminate as many overhead / irrelevant codes as possible and hopefully get more consistent data.

+5
source

Which profiler did you use? XDebug?

I would suspect that this is a problem with the profiler, as you are showing a fairly significant difference. See if these profiles are different ...

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

All Articles