Anyways, a while back, there were these threads.
Can PHP realize something like structure in C
http://www.devnetwork.net/forums/viewtopic.php?p=8841
and
Structures in PHP revisited
http://www.devnetwork.net/forums/viewtopic.php?t=2677
Well, Matt Wade had created some benchmarking code to check the speed of the findings we came up with in the first thread. It pretty much confirmed the suspicion that mimicing the use of a structure in a class would be slower than doing so with an array.
Well,
Once again, after finishing a project here in the office, I was ramping up for another but got back to thinking about this stuff and found something extremeley interesting. In short, the performance of associative arrays are terrible!
Take a look at the code below.
Code: Select all
<?
# Let's make sure we don't time out
set_time_limit(0);
function getmicrotime()
{
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}
class Struct
{
var $count = 0;
var $count2 = 0;
var $count3 = 0;
}
function Struct_arr_index()
{
$struct_array=array(
0 => 0,
2 => 0,
3 => 0
);
return $struct_array;
}
function Struct_arr_assoc()
{
$struct_array=array(
count => 0,
count2 => 0,
count3 => 0
);
return $struct_array;
}
# Test one. Using a class.
$struct = new Struct;
$time_start = getmicrotime();
for($i = 0;$i < 100000; $i++)
{
$struct->count3 = $i;
}
$time_end = getmicrotime();
echo "Using a class took: " . ($time_end - $time_start) . " seconds.<BR>\n\n";
# End test one.
# Test two. Using an indexed array.
$array=Struct_arr_index();
$time_start = getmicrotime();
for($i = 0;$i < 100000; $i++)
{
$arrayї3] = $i;
}
$time_end = getmicrotime();
echo "Using an indexed array took: " . ($time_end - $time_start) . " seconds.<BR>\n\n";
# End test two.
# Test three. Using a plain array.
$array2 = Struct_arr_assoc();
$time_start = getmicrotime();
for($i = 0;$i < 100000; $i++)
{
$array2їcount3] = $i;
}
$time_end = getmicrotime();
echo "Using an associative array took: " . ($time_end - $time_start) . " seconds.<BR>\n\n";
# End test three.
?>Now for giggles, I ran the script again as such.
Code: Select all
<?
# Let's make sure we don't time out
set_time_limit(0);
function getmicrotime()
{
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}
class Struct
{
var $count = 0;
var $count2 = 0;
var $count3 = 0;
}
function Struct_arr_index()
{
$struct_array=array(
0 => 0,
2 => 0,
3 => 0
);
return $struct_array;
}
function Struct_arr_assoc()
{
$struct_array=array(
count => 0,
count2 => 0,
count3 => 0
);
return $struct_array;
}
# Test one. Using a class.
$struct = new Struct;
$time_start = getmicrotime();
for($i = 0;$i < 100000; $i++)
{
$struct->count3 = $i;
++$struct->count;
}
$time_end = getmicrotime();
echo "Using a class took: " . ($time_end - $time_start) . " seconds.<BR>\n\n";
# End test one.
# Test two. Using an indexed array.
$array=Struct_arr_index();
$time_start = getmicrotime();
for($i = 0;$i < 100000; $i++)
{
$arrayї3] = $i;
++$arrayї0];
}
$time_end = getmicrotime();
echo "Using an indexed array took: " . ($time_end - $time_start) . " seconds.<BR>\n\n";
# End test two.
# Test three. Using a plain array.
$array2 = Struct_arr_assoc();
$time_start = getmicrotime();
for($i = 0;$i < 100000; $i++)
{
$array2їcount3] = $i;
++$array2їcount];
}
$time_end = getmicrotime();
echo "Using an associative array took: " . ($time_end - $time_start) . " seconds.<BR>\n\n";
# End test three.
?>That's crazy!
To summarize, if you are interested in mimicing the use of structures in PHP, the class approach is the best! If you are simply worried about speed at the expense of readability, then the Struct_array_index() function is what you want.
Now comparing the first two, I have to say that I would rather be able to use a name for an element than a number. The code is easier to read and maintain. I'll take the class approach.
If performance really is a concern, and it should be, then stay away from the third option as much as possible. It's slow as Christmast and the benchmarking bears this out.
Later on,
BDKR