Russell (zimzat) wrote,
Russell
zimzat

  • Mood:
  • Music:

Some PHP profiling results

I was just doing some profiling code in PHP so I figured I would share the results.

if-else is better than $var = (condition) ? true : false; 2/3 of the time. Tested 30 times at 2,000,000 times each. Difference was negligable in all cases.

using list() = each() is better than foreach() if you process the same array more than once, otherwise foreach() is better at least 3/4 of the time.
Turns out this was an error on my part. foreach() automatically resets the array before it starts, while list() = each() doesn't, thus in my code it said "We're at the end. We're done already" and stopped. After adding a manual reset() the foreach() code was always faster.

foreach() is better than for()'ing through array_keys() 29 times out of 30. Last time was probably due to hiccup in system.

You can download the profiler code here. It's set up to allow you to add a couple of functions and then compare their execution time X number of times (10 being default X). Very simple.
(updated file on server to reflect changes in code)
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

  • 11 comments