大多数时间数组与类的测试只测试实例化它们。一旦你真正开始对他们做某事。
我是一个只使用数组的“纯粹主义者”,因为性能要好得多。我写了下面的代码来证明自己是为了证明不使用类的额外麻烦是合理的(即使它们对程序员来说更容易)
可以说我对结果感到非常惊讶!
<?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']);
}
//-------------------------------------
?>
测试读数:1
测试作业:1
测试全局变量:1
测试功能:1
阵列时间 = 1.58905816078
上课时间 = 1.11980104446
总阵列时间(到目前为止)= 1.58903813362(100,000 次迭代)
总课程时间(到目前为止)= 1.11979603767(100,000 次迭代)
阵列时间 = 1.02581000328
上课时间 = 1.22492313385
总阵列时间(到目前为止)= 2.61484408379(100,000 次迭代)
总课程时间(到目前为止)= 2.34471416473(100,000 次迭代)
阵列时间 = 1.29942297935
上课时间 = 1.18844485283
总阵列时间(到目前为止)= 3.91425895691(100,000 次迭代)
总课程时间(到目前为止)= 3.5331492424(100,000 次迭代)
阵列时间 = 1.28776097298
上课时间 = 1.02383089066
总阵列时间(到目前为止)= 5.2020149231(100,000 次迭代)
总课程时间(到目前为止)= 4.55697512627(100,000 次迭代)
阵列时间 = 1.31235599518
上课时间 = 1.38880181313
总阵列时间(到目前为止)= 6.51436591148(100,000 次迭代)
总课程时间(到目前为止)= 5.94577097893(100,000 次迭代)
阵列时间 = 1.3007349968
上课时间 = 1.07644081116
总阵列时间(到目前为止)= 7.81509685516(100,000 次迭代)
总课程时间(到目前为止)= 7.02220678329(100,000 次迭代)
阵列时间 = 1.12752890587
上课时间 = 1.07106018066
总阵列时间(到目前为止)= 8.94262075424(100,000 次迭代)
总课程时间(到目前为止)= 8.09326195717(100,000 次迭代)
阵列时间 = 1.08890199661
上课时间 = 1.09139609337
总阵列时间(到目前为止)= 10.0315177441(100,000 次迭代)
总课程时间(到目前为止)= 9.18465089798(100,000 次迭代)
阵列时间 = 1.6172170639
上课时间 = 1.14714384079
总阵列时间(到目前为止)= 11.6487307549(100,000 次迭代)
总课程时间(到目前为止)= 10.3317887783(100,000 次迭代)
阵列时间 = 1.53738498688
上课时间 = 1.28127002716
总阵列时间(到目前为止)= 13.1861097813(100,000 次迭代)
总课程时间(到目前为止)= 11.6130547523(100,000 次迭代)
总次数:
总阵列时间 = 13.1861097813(1,000,000 次迭代)
总课程时间 = 11.6130547523(1,000,000 次迭代)
因此,无论哪种方式,差异都可以忽略不计。我很惊讶地发现,一旦你开始在全局范围内访问东西,类实际上会变得更快。
但是不要相信我,为你自己运行它。我个人现在对在我的高性能应用程序中使用类完全感到内疚。 :D