[development] Time for a new chapter of an endless debate
Angela Byron
drupal-devel at webchick.net
Mon Jul 3 02:15:30 UTC 2006
chx helped me figure out how to benchmark tonight, so here are some
results from some random tests. Tested on Mac OS X 10.4 and MAMP
1.2.1. The table in question for the fetch tests had about 2,500
records in it:
PHP 4.4.2:
Initialize 1 million objects: 1192.52 ms
Initialize 1 million arrays: 735.15ms
Fetch object: 2.34ms
Fetch array: 0.81ms
Make object with 1,000 properties: 3ms
Make array with 1,000 keys: 2.51ms
Foreach through 1,000 object properties: 3.78ms
Foreach through 1,000 array keys: 2.6ms
PHP 5.1.2:
Initialize 1 million objects: 811.72 ms
Initialize 1 million arrays: 281.83ms
Fetch object: 0.94ms
Fetch array: 0.72ms
Make object with 1,000 properties: 1.77ms
Make array with 1,000 keys: 1.38ms
Foreach through 1,000 object properties: 0.98ms
Foreach through 1,000 array keys: 1.21ms
My horrible script is below. ;)
Looks like PHP 5 did indeed speed up stuff. In PHP 4, arrays are
faster across the board, but in PHP 5 it's somewhat mixed. That said,
these can hardly be called "real world" tests :P but I found it kind
of interesting.
---
<?php
include 'includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
echo "Initialize 1 million objects: ";
timer_start('object');
for ($i = 0; $i < 1000000; $i++) {
$object = new stdClass();
}
$stop = timer_stop('object');
echo $stop['time'] ." ms<br />";
echo "Initialize 1 million arrays: ";
timer_start('array');
for ($i = 0; $i < 1000000; $i++) {
$array = array();
}
$stop = timer_stop('array');
echo $stop['time'] .'ms <br />';
echo "Fetch object: ";
timer_start('fetch_object');
$result = db_query_temporary("SELECT * FROM {node_revision}");
$i = 0;
while ($object = db_fetch_object($result)) {
$i++;
}
$stop = timer_stop('fetch_object');
echo $stop['time'] .'ms <br />';
echo "Fetch array: ";
timer_start('fetch_array');
$result = db_query_temporary("SELECT * FROM {node_revision}");
$i = 0;
while ($array = db_fetch_array($result)) {
$i++;
}
$stop = timer_stop('fetch_array');
echo $stop['time'] .'ms <br />';
echo "Make object with 1,000 properties: ";
timer_start('big_object');
$object = new stdClass();
for ($i = 0; $i < 1000; $i++) {
$prop = (string)$i;
$object->$prop = $i;
}
$stop = timer_stop('big_object');
echo $stop['time'] .'ms <br />';
echo "Make array with 1,000 keys: ";
timer_start('big_array');
$array = array();
for ($i = 0; $i < 1000; $i++) {
$key = (string)$i;
$array[$key] = $i;
}
$stop = timer_stop('big_array');
echo $stop['time'] .'ms <br />';
echo "Foreach through 1,000 object properties: ";
timer_start('foreach_object');
$i = 0;
foreach ($object as $prop => $value) {
$object->$prop = $value++;
}
$stop = timer_stop('foreach_object');
echo $stop['time'] .'ms <br />';
echo "Foreach through 1,000 array keys: ";
timer_start('foreach_array');
$i = 0;
foreach ($array as $key => $value) {
$array[$key] = $value++;
}
$stop = timer_stop('foreach_array');
echo $stop['time'] .'ms <br />';
?>
More information about the development
mailing list