<?php
$memory1 = microtime( TRUE );
$data = array();
for($i=0;$i<1000;$i++) {
$data[] = array(
'property1' => md5(microtime()),
'property2' => md5(microtime()),
'property3' => md5(microtime()),
);
}
$array = microtime( TRUE )- $memory1 . "\n";
// Now do the same thing, but with a class..
class Test {
public $property1;
public $property2;
public $property3;
}
class Data {
}
$data = array();
$memory1 = microtime( TRUE );
$data = new Data();
for($i=0;$i<1000;$i++) {
$test = new Test();
$test->property1 = md5(microtime());
$test->property2 = md5(microtime());
$test->property3 = md5(microtime());
$data->$i = $test;
}
$object = microtime( TRUE )-$memory1;
echo 'Arrays: ' . $first = floor($array * 1000000) . "\n";
echo 'Objects: ' . $second = floor($object * 1000000) . "\n" ;
echo ($first-$second)/$first;
?>
结论: 对象比数组提升性能一般在2千分之- 20%