Apr 26

MySQL Planning to Go Public

MySQL has reached the $50 million revenue mark and is gearing up towards an IPO. This could prove to be an interesting time for MySQL and the open source database world for web 2.0. I am interested in what lay ahead, how MySQL could be affected and whether it may be a good investment opportunity ;).

Read more from my post at the NFi Studios Blog.

Apr 19

PHP For vs. Foreach

I have always believed that iterating an array using a foreach loop in php was slower than iterating through the same array using a standard for loop. I figured this because there is no foreach equivalent in the underlying native code that php is run on and therefore a standard for loop didn’t require any additional translations.

I have never read any concrete evidence as to which is faster and a quick google search didn’t provide any results. Thus I decided to setup a benchmark test myself. The question in mind for me was “Is it more efficient to loop through a given array with a for or foreach loop?” This is also assuming a standard indexed array and not an associative array or hash table. since you would pretty much only use a foreach for that anyway.

The test involved first filling an array 100,000 items by just counting up from zero. A for loop counting to 100,000 was then setup along with a foreach loop set to iterate through the pre-filled array. An extra loop was setup around each of the test loops to do the iterations multiple times. I like to do this to get more of an average running time because using time as a measurement in a multitasking environment isn’t exactly 100% accurate. The body of each loop was merely a continue statement to move on to the next cycle.
Here is the php code:


set_time_limit(0);
$array = array();
echo "Filling array\n";

for($index=0; $index < 100000; $index++)
{
     $array[$index] = $index;
} 

$start = time();
for($iteration = 0;$iteration < 10; $iteration++)
{
     for($index=0;$index < 100000;$index++)
     {
          continue;
     }
}
$end = time();

$duration = $end - $start;
echo "For loop: ".$duration;$start = time();

for($iteration=0; $iteration < 10; $iteration++)
{
     foreach ($array as $value)
     {
           continue;
     }
}
$end = time();

$duration = $end - $start; echo "\nForeach loop: ".$duration; 

The results went against what I originally thought about php. I changed the iteration counts and the array size to make sure there wasn’t any affect due to loop setups or such. The results were nearly always the same. The for loop took twice as long to iterate than the foreach. Performance with any code is highly dependent on the context it is used in (especially in php) but this shows me my original thoughts were wrong.

Results for the above:

Filling array
For loop: 21
Foreach loop: 11

The benchmark was run on a Macbook Pro 1.8 Ghz Core Duo with 1.5GB ram. The test was run via CLI on php version 5.2.0. No optimizers or opcode cache was used. Try it out on different configurations and post your results in the comments.