Most tests that temporary arrays against classes only test them. As soon as you start to do something with them.
I was a purist who only used arrays because the performance was much better. I wrote the following code to justify myself, to justify the extra difficulties of using classes (even if they are simpler than a programmer)
Let's say I was VERY surprised by the results!
<?php $rx = ""; $rt = ""; $rf = ""; $ta = 0; // total array time $tc = 0; // total class time // flip these to test different attributes $test_globals = true; $test_functions = true; $test_assignments = true; $test_reads = true; // define class class TestObject { public $a; public $b; public $c; public $d; public $e; public $f; public function __construct($a,$b,$c,$d,$e,$f) { $this->a = $a; $this->b = $b; $this->c = $c; $this->d = $d; $this->e = $e; $this->f = $f; } public function setAtoB() { $this->a = $this->b; } } // begin test echo "<br>test reads: " . $test_reads; echo "<br>test assignments: " . $test_assignments; echo "<br>test globals: " . $test_globals; echo "<br>test functions: " . $test_functions; echo "<br>"; for ($z=0;$z<10;$z++) { $starta = microtime(true); for ($x=0;$x<100000;$x++) { $xr = getArray('aaa','bbb','ccccccccc','ddddddddd','eeeeeeee','fffffffffff'); if ($test_assignments) { $xr['e'] = "e"; $xr['c'] = "sea biscut"; } if ($test_reads) { $rt = $x['b']; $rx = $x['f']; } if ($test_functions) { setArrAtoB($xr); } if ($test_globals) { $rf = glb_arr(); } } $ta = $ta + (microtime(true)-$starta); echo "<br/>Array time = " . (microtime(true)-$starta) . "\n\n"; $startc = microtime(true); for ($x=0;$x<100000;$x++) { $xo = new TestObject('aaa','bbb','ccccccccc','ddddddddd','eeeeeeee','fffffffffff'); if ($test_assignments) { $xo->e = "e"; $xo->c = "sea biscut"; } if ($test_reads) { $rt = $xo->b; $rx = $xo->f; } if ($test_functions) { $xo->setAtoB(); } if ($test_globals) { $xf = glb_cls(); } } $tc = $tc + (microtime(true)-$startc); echo "<br>Class time = " . (microtime(true)-$startc) . "\n\n"; echo "<br>"; echo "<br>Total Array time (so far) = " . $ta . "(100,000 iterations) \n\n"; echo "<br>Total Class time (so far) = " . $tc . "(100,000 iterations) \n\n"; echo "<br>"; } echo "TOTAL TIMES:"; echo "<br>"; echo "<br>Total Array time = " . $ta . "(1,000,000 iterations) \n\n"; echo "<br>Total Class time = " . $tc . "(1,000,000 iterations)\n\n"; // test functions function getArray($a,$b,$c,$d,$e,$f) { $arr = array(); $arr['a'] = $a; $arr['b'] = $b; $arr['c'] = $c; $arr['d'] = $d; $arr['d'] = $e; $arr['d'] = $f; return($arr); } //------------------------------------- function setArrAtoB($r) { $r['a'] = $r['b']; } //------------------------------------- function glb_cls() { global $xo; $xo->d = "ddxxdd"; return ($xo->f); } //------------------------------------- function glb_arr() { global $xr; $xr['d'] = "ddxxdd"; return ($xr['f']); } //------------------------------------- ?>
test reads: 1 test items: 1 test global values: 1 test functions: 1
Array time = 1.58905816078 Time class = 1.11980104446 Total array time (so far) = 1.58903813362 (100,000 iterations) Total class time (still) = 1.11979603767 (100,000 iterations)
Array time = 1.02581000328 Time class = 1.22492313385 Total array time (so far) = 2.61484408379 (100,000 iterations) Total class time (still) = 2.34471416473 (100,000 iterations)
Array time = 1.29942297935 Time class = 1.18844485283 Total array time (so far) = 3.91425895691 (100,000 iterations) Total class time (still) = 3,5331492424 (100,000 iterations)
Array time = 1.28776097298 Time class = 1.02383089066 Total array time (for now) = 5.2020149231 (100,000 iterations) Total class time (still) = 4.55697512627 (100,000 iterations)
Array time = 1.31235599518 Occupation time = 1.38880181313 Total array time (still) = 6.51436591148 (100,000 iterations) Total class time (still) = 5.94577097893 (100,000 iterations)
Array time = 1.3007349968 Time class = 1.07644081116 Total array time (so far) = 7.81509685516 (100,000 iterations) Total class time (still) = 7.02220678329 (100,000 iterations)
Array time = 1.12752890587 Time class = 1.07106018066 Total array time (still) = 8.94262075424 (100,000 iterations) Total class time (still) = 8.09326195717 (100,000 iterations)
Array time = 1.08890199661 Time class = 1.09139609337 Total array time (so far) = 10.0315177441 (100,000 iterations) Total class time (still) = 9.18465089798 (100,000 iterations)
Array time = 1.6172170639 Class time = 1.14714384079 Total array time (so far) = 11.6487307549 (100,000 iterations) Total class time (still) = 10.3317887783 (100,000 iterations)
Array time = 1.53738498688 Class time = 1.28127002716 Total array time (until now) = 13.1861097813 (100,000 iterations) Total class time (still) = 11.6130547523 (100,000 iterations)
TOTAL TIME: Total array time = 13.1861097813 (1,000,000 iterations) Total Class time = 11.6130547523 (1,000,000 iterations)
So, in any case, the difference is pretty slight. I was very surprised to find that as soon as you start accessing things all over the world, classes will really get a little faster.
But do not believe me, run it for yourself. I personally now feel completely guilty of using classes in my high-performance applications .: D