我有一个神经网络的大量PHP对象,我必须迭代它并执行一些数学运算。我想知道我是否会更好地在类的实例上使用关联数组?
我正在处理大约3640
个对象,并在此基础上迭代500
次(最好),因此任何微优化都有很大帮助。 $object['value']
比$object->value
不可避免地更快吗?
编辑:所以他们都是一样的。但我想构造函数会有一点开销吗?无论哪种方式,我都不认为我想在我的漂亮类中交换脏数组:P
答案 0 :(得分:62)
基于Quazzle的代码,我运行了下一个代码(5.4.16 windows 64bits):
<?php
class SomeClass {
public $aaa;
public $bbb;
public $ccc;
}
function p($i) {
echo '<pre>';
print_r($i);
echo '</pre>';
}
$t0 = microtime(true);
$arraysOf=array();
$inicio=memory_get_usage();
for ($i=0; $i<1000; $i++) {
$z = array();
for ($j=0; $j<1000; $j++) {
$z['aaa'] = 'aaa';
$z['bbb'] = 'bbb';
$z['ccc'] = $z['aaa'].$z['bbb'];
}
$arraysOf[]=$z;
}
$fin=memory_get_usage();
echo '<p>arrays: '.(microtime(true) - $t0)."</p>";
echo '<p>memory: '.($fin-$inicio)."</p>";
p($z);
$t0 = microtime(true);
$arraysOf=array();
$inicio=memory_get_usage();
for ($i=0; $i<1000; $i++) {
$z = new SomeClass();
for ($j=0; $j<1000; $j++) {
$z->aaa = 'aaa';
$z->bbb = 'bbb';
$z->ccc = $z->aaa.$z->bbb;
}
$arraysOf[]=$z;
}
$fin=memory_get_usage();
echo '<p>arrays: '.(microtime(true) - $t0)."</p>";
echo '<p>memory: '.($fin-$inicio)."</p>";
p($z);
$t0 = microtime(true);
$arraysOf=array();
$inicio=memory_get_usage();
for ($i=0; $i<1000; $i++) {
$z = new stdClass();
for ($j=0; $j<1000; $j++) {
$z->aaa = 'aaa';
$z->bbb = 'bbb';
$z->ccc = $z->aaa.$z->bbb;
}
$arraysOf[]=$z;
}
$fin=memory_get_usage();
echo '<p>arrays: '.(microtime(true) - $t0)."</p>";
echo '<p>memory: '.($fin-$inicio)."</p>";
p($z);
?>
我获得了下一个结果:
arrays: 1.8451430797577
memory: 460416
Array
(
[aaa] => aaa
[bbb] => bbb
[ccc] => aaabbb
)
arrays: 1.8294548988342
memory: 275696
SomeClass Object
(
[aaa] => aaa
[bbb] => bbb
[ccc] => aaabbb
)
arrays: 2.2577090263367
memory: 483648
stdClass Object
(
[aaa] => aaa
[bbb] => bbb
[ccc] => aaabbb
)
php 5.4的结论
ps:作为一个注释,如果定义了类但是成员那么,这个类的使用速度会慢一些。它还使用更多内存。 显然秘诀是定义成员
我从php 5.4更新到php 5.5(5.5.12 x86 windows)。
arrays: 1.6465699672699
memory: 460400
Array
(
[aaa] => aaa
[bbb] => bbb
[ccc] => aaabbb
)
arrays: 1.8687851428986
memory: 363704
SplFixedArray Object
(
[0] => aaa
[1] => bbb
[2] => aaabbb
)
arrays: 1.8554251194
memory: 275568
SomeClass Object
(
[aaa] => aaa
[bbb] => bbb
[ccc] => aaabbb
)
arrays: 2.0101680755615
memory: 483656
stdClass Object
(
[aaa] => aaa
[bbb] => bbb
[ccc] => aaabbb
)
答案 1 :(得分:9)
我使用此代码进行“分析”(1000个实例,1000.000个读/写):
function p($i) {
echo '<pre>';
print_r($i);
echo '</pre>';
}
$t0 = microtime(true);
for ($i=0; $i<1000; $i++) {
$z = array();
for ($j=0; $j<1000; $j++) {
$z['aaa'] = 'aaa';
$z['bbb'] = 'bbb';
$z['ccc'] = $z['aaa'].$z['bbb'];
}
}
echo '<p>arrays: '.(microtime(true) - $t0);
p($z);
$t0 = microtime(true);
for ($i=0; $i<1000; $i++) {
$z = (object) null;
for ($j=0; $j<1000; $j++) {
$z->aaa = 'aaa';
$z->bbb = 'bbb';
$z->ccc = $z->aaa.$z->bbb;
}
}
echo '<p>obj: '.(microtime(true) - $t0);
p($z);
echo '<p> phpversion '.phpversion();
它在我的LINUX输出托管这个东西:
arrays: 1.1085488796234
Array
(
[aaa] => aaa
[bbb] => bbb
[ccc] => aaabbb
)
obj: 1.2824709415436
stdClass Object
(
[aaa] => aaa
[bbb] => bbb
[ccc] => aaabbb
)
phpversion 5.2.17
所以总结:即使在PHP 5.2上,对象也会变慢。除非你真的需要它们的oop功能,否则不要使用对象。
答案 2 :(得分:3)
我在php 7.0.9下使用magallanes的代码:
arrays: 0.19802498817444
memory: 324672
Array
(
[aaa] => aaa
[bbb] => bbb
[ccc] => aaabbb
)
arrays: 0.18602299690247
memory: 132376
SomeClass Object
(
[aaa] => aaa
[bbb] => bbb
[ccc] => aaabbb
)
arrays: 0.1950249671936
memory: 348296
stdClass Object
(
[aaa] => aaa
[bbb] => bbb
[ccc] => aaabbb
)
用户php 7.1.3:
arrays: 0.59932994842529
memory: 444920
Array
(
[aaa] => aaa
[bbb] => bbb
[ccc] => aaabbb
)
arrays: 0.72895789146423
memory: 164512
SomeClass Object
(
[aaa] => aaa
[bbb] => bbb
[ccc] => aaabbb
)
arrays: 0.61777496337891
memory: 484416
stdClass Object
(
[aaa] => aaa
[bbb] => bbb
[ccc] => aaabbb
)
答案 3 :(得分:2)
你没有向我们展示$object->value
如何工作的代码,因为它可能是后端它是一个数组,在这种情况下理论上使用数组会更快,因为它涉及少一个函数调用。与函数调用相比,执行查找的成本可能很高。如果它是一个变量,那么差异就会很小,因为PHP中的对象和数组具有非常相似的实现。
如果您正在考虑优化,则需要进行配置以检查大部分时间的使用位置。我怀疑将对象更改为数组不会产生重大影响。
答案 4 :(得分:2)
我觉得这是一篇很老的帖子,所以我想我会更新它。 这是我的代码和统计数据,在Zend CE 5.3.21上完成 我试着测试整个东西,存储信息并将其拉回来。
V1:需要0.83秒
for ($i=1; $i<1000000; $i++) {
$a = get_one();
$b = $a[0];
$b = $a[1];
}
function get_one() {
return array(1,1);
}
V2:需要3.05秒
for ($i=1; $i<1000000; $i++) {
$a = get_one();
$b = $a->v;
$b = $a->k;
}
function get_one() {
$ret = new test();
$ret->v = 1;
$reb->k = 1;
return $ret;
}
class test {
public $v;
public $k;
}
V3:需要1.98秒(注意构造函数可以提高性能)
for ($i=1; $i<1000000; $i++) {
$a = get_one();
$b = $a->v;
$b = $a->k;
}
function get_one() {
return new test(1,1);
}
class test {
public $v;
public $k;
public function __construct($v, $k) {
$this->v = $v;
$this->k = $k;
}
}
答案 5 :(得分:2)
好吧,今天我对@magallanes基准感到好奇,因此我对其进行了扩展。我增加了一些for循环,以真正突出显示事物之间的差距。它可以在Apache 2.4,mod_php和PHP 7.2上运行。
下面是一个汇总表,可简化结果:
+---------------------------+---------+-----------------+
| Test | Memory | Time |
+---------------------------+---------+-----------------+
| Array | 2305848 | 9.5637300014496 |
| stdClass | 2505824 | 11.212271928787 |
| SomeClass | 963640 | 11.558017015457 | <-- *
| AnotherClass | 2563136 | 10.872401237488 |
| SetterClass | 905848 | 59.879059791565 |
| SetterClassDefineReturn | 905792 | 60.484427213669 |
| SetterClassSetFromParam | 745792 | 62.783381223679 |
| SetterClassSetKeyAndParam | 745824 | 72.155715942383 |
+---------------------------+---------+-----------------+
* - Winner winner chicken dinner
下面是修改后的脚本。我想用方法和定义类型来测试设置属性。我很惊讶地发现使用setter方法会对代码产生重大影响。现在,这是一个非常非常具体的性能测试,许多应用程序甚至都不会达到这个目的。但是,如果您有一个处理1000个类的1000个类的站点,该站点处理1000 / reqs /秒,那么您会发现这可能对性能有何影响。
<?php
set_time_limit(500);
class SomeClass {
public $aaa;
public $bbb;
public $ccc;
}
class AnotherClass {
}
class SetterClass {
public $aaa;
public $bbb;
public $ccc;
public function setAAA() {
$this->aaa = 'aaa';
}
public function setBBB() {
$this->bbb = 'bbb';
}
public function setCCC() {
$this->ccc = $this->aaa.$this->bbb;
}
}
class SetterClassDefineReturn {
public $aaa;
public $bbb;
public $ccc;
public function setAAA():void {
$this->aaa = 'aaa';
}
public function setBBB():void {
$this->bbb = 'bbb';
}
public function setCCC():void {
$this->ccc = $this->aaa.$this->bbb;
}
}
class SetterClassSetFromParam {
public $aaa;
public $bbb;
public $ccc;
public function setAAA(string $val): void {
$this->aaa = $val;
}
public function setBBB(string $val): void {
$this->bbb = $val;
}
public function setCCC(string $val): void {
$this->ccc = $val;
}
}
class SetterClassSetKeyAndParam {
public $aaa;
public $bbb;
public $ccc;
public function set(string $key, string $val): void {
$this->{$key} = $val;
}
}
function p($i) {
echo '<pre>';
print_r($i);
echo '</pre>';
echo '<hr>';
}
$t0 = microtime(true);
$arraysOf=[];
$inicio=memory_get_usage();
for ($i=0; $i<5000; $i++) {
$z = new SomeClass();
for ($j=0; $j<5000; $j++) {
$z->aaa = 'aaa';
$z->bbb = 'bbb';
$z->ccc = $z->aaa.$z->bbb;
}
$arraysOf[]=$z;
}
$fin=memory_get_usage();
echo '<p>Time Taken (seconds): '.(microtime(true) - $t0).'</p>';
echo '<p>Memory: '.($fin-$inicio).'</p>';
p($z);
$t0 = microtime(true);
$arraysOf=[];
$inicio=memory_get_usage();
for ($i=0; $i<5000; $i++) {
$z = new AnotherClass();
for ($j=0; $j<5000; $j++) {
$z->aaa = 'aaa';
$z->bbb = 'bbb';
$z->ccc = $z->aaa.$z->bbb;
}
$arraysOf[]=$z;
}
$fin=memory_get_usage();
echo '<p>Time Taken (seconds): '.(microtime(true) - $t0).'</p>';
echo '<p>Memory: '.($fin-$inicio).'</p>';
p($z);
$t0 = microtime(true);
$arraysOf=[];
$inicio=memory_get_usage();
for ($i=0; $i<5000; $i++) {
$z = new SetterClass();
for ($j=0; $j<5000; $j++) {
$z->setAAA();
$z->setBBB();
$z->setCCC();
}
$arraysOf[]=$z;
}
$fin=memory_get_usage();
echo '<p>Time Taken (seconds): '.(microtime(true) - $t0).'</p>';
echo '<p>Memory: '.($fin-$inicio).'</p>';
p($z);
$t0 = microtime(true);
$arraysOf=[];
$inicio=memory_get_usage();
for ($i=0; $i<5000; $i++) {
$z = new SetterClassDefineReturn();
for ($j=0; $j<5000; $j++) {
$z->setAAA();
$z->setBBB();
$z->setCCC();
}
$arraysOf[]=$z;
}
$fin=memory_get_usage();
echo '<p>Time Taken (seconds): '.(microtime(true) - $t0).'</p>';
echo '<p>Memory: '.($fin-$inicio).'</p>';
p($z);
$t0 = microtime(true);
$arraysOf=[];
$inicio=memory_get_usage();
for ($i=0; $i<5000; $i++) {
$z = new SetterClassSetFromParam();
for ($j=0; $j<5000; $j++) {
$z->setAAA('aaa');
$z->setBBB('bbb');
$z->setCCC('aaabbb');
}
$arraysOf[]=$z;
}
$fin=memory_get_usage();
echo '<p>Time Taken (seconds): '.(microtime(true) - $t0).'</p>';
echo '<p>Memory: '.($fin-$inicio).'</p>';
p($z);
$t0 = microtime(true);
$arraysOf=[];
$inicio=memory_get_usage();
for ($i=0; $i<5000; $i++) {
$z = new SetterClassSetKeyAndParam();
for ($j=0; $j<5000; $j++) {
$z->set('aaa', 'aaa');
$z->set('bbb', 'bbb');
$z->set('ccc', 'aaabbb');
}
$arraysOf[]=$z;
}
$fin=memory_get_usage();
echo '<p>Time Taken (seconds): '.(microtime(true) - $t0).'</p>';
echo '<p>Memory: '.($fin-$inicio).'</p>';
p($z);
$t0 = microtime(true);
$arraysOf=[];
$inicio=memory_get_usage();
for ($i=0; $i<5000; $i++) {
$z = new stdClass();
for ($j=0; $j<5000; $j++) {
$z->aaa = 'aaa';
$z->bbb = 'bbb';
$z->ccc = $z->aaa.$z->bbb;
}
$arraysOf[]=$z;
}
$fin=memory_get_usage();
echo '<p>Time Taken (seconds): '.(microtime(true) - $t0).'</p>';
echo '<p>Memory: '.($fin-$inicio).'</p>';
p($z);
$t0 = microtime(true);
$arraysOf=[];
$inicio=memory_get_usage();
for ($i=0; $i<5000; $i++) {
$z = [];
for ($j=0; $j<5000; $j++) {
$z['aaa'] = 'aaa';
$z['bbb'] = 'bbb';
$z['ccc'] = $z['aaa'].$z['bbb'];
}
$arraysOf[]=$z;
}
$fin=memory_get_usage();
echo '<p>Time Taken (seconds): '.(microtime(true) - $t0).'</p>';
echo '<p>Memory: '.($fin-$inicio).'</p>';
p($z);
结果如下:
Time Taken (seconds): 11.558017015457
Memory: 963640
SomeClass Object
(
[aaa] => aaa
[bbb] => bbb
[ccc] => aaabbb
)
-----
Time Taken (seconds): 10.872401237488
Memory: 2563136
AnotherClass Object
(
[aaa] => aaa
[bbb] => bbb
[ccc] => aaabbb
)
----
Time Taken (seconds): 59.879059791565
Memory: 905848
SetterClass Object
(
[aaa] => aaa
[bbb] => bbb
[ccc] => aaabbb
)
----
Time Taken (seconds): 60.484427213669
Memory: 905792
SetterClassDefineReturn Object
(
[aaa] => aaa
[bbb] => bbb
[ccc] => aaabbb
)
----
Time Taken (seconds): 62.783381223679
Memory: 745792
SetterClassSetFromParam Object
(
[aaa] => aaa
[bbb] => bbb
[ccc] => aaabbb
)
----
Time Taken (seconds): 72.155715942383
Memory: 745824
SetterClassSetKeyAndParam Object
(
[aaa] => aaa
[bbb] => bbb
[ccc] => aaabbb
)
----
Time Taken (seconds): 11.212271928787
Memory: 2505824
stdClass Object
(
[aaa] => aaa
[bbb] => bbb
[ccc] => aaabbb
)
----
Time Taken (seconds): 9.5637300014496
Memory: 2305848
Array
(
[aaa] => aaa
[bbb] => bbb
[ccc] => aaabbb
)
答案 6 :(得分:1)
您可以随时查看PHP源代码,了解微观性能等功能。
但是乍一看,没有做['value']也不会更快,因为PHP需要查找找到['value']的位置,即使哈希表查找应该是O(1),那不是保证。使用Text-index时会有更多的开销。
如果对象只包含1个需要访问的变量值,那么使用对象会产生更多的开销。
答案 7 :(得分:1)
对于仍对该问题感兴趣的任何人:) 我在PHP 7.1 Ubuntu x64上运行Quazzle代码并得到以下答案:
arrays: 0.24848890304565
memory: 444920
Array
(
[aaa] => aaa
[bbb] => bbb
[ccc] => aaabbb
)
arrays: 0.23238587379456
memory: 164512
SomeClass Object
(
[aaa] => aaa
[bbb] => bbb
[ccc] => aaabbb
)
arrays: 0.24422693252563
memory: 484416
stdClass Object
(
[aaa] => aaa
[bbb] => bbb
[ccc] => aaabbb
)
数组比类对象占用 4 (!)。
类对象快一些。
stdClass仍然很邪恶©magallanes:)
答案 8 :(得分:0)
如果Arrays和Class的性能相同,我认为使用预定义类的对象来存储/传递业务数据会使我们的程序更具逻辑性,代码更具可读性。
今天,有了像Eclipse,Netbean这样的现代ide ......了解对象(预定义类)携带的信息非常方便,但数组并非如此
例如: 带数组
function registerCourse(array $student) {
// Right here I don't know how a $student look like unless doing a print_r() or var_dump()
....
}
使用对象
class Studen {
private $_name, $_age;
public function getAge() {}
public function getName() {}
..
}
function registerCourse(Studen $student) {
// Right here I just Ctrl+Space $student or click "Student" and I know I can get name or age from it
...
}
答案 9 :(得分:0)
magallanes的脚本@ PHP 7.3.5
SomeClass Object
最快,最轻。Array
1.32倍速度。 2.70x 内存。stdClass Object
1.65倍速度。 2.94倍内存。原始输出:
arrays: 0.064794063568115
memory: 444920
Array (
[aaa] => aaa
[bbb] => bbb
[ccc] => aaabbb
)
arrays: 0.048975944519043
memory: 164512
SomeClass Object (
[aaa] => aaa
[bbb] => bbb
[ccc] => aaabbb
)
arrays: 0.081161022186279
memory: 484416
stdClass Object (
[aaa] => aaa
[bbb] => bbb
[ccc] => aaabbb
)
答案 10 :(得分:0)
为了公平起见,这取决于您拥有哪种数据。其他答案中的结果(或多或少)是正确的。为了清楚起见,我将在底部粘贴测试代码。
这里重要的是,如果您使用预定义的对象,则可以节省大量内存,但要权衡的是您需要定义确切的结构。
请考虑以下测试结果:
static obj: 0.04223108291626
memory: 164920
object(staticSet)#1 (3) {
["aaa"]=>
string(3) "aaa"
["bbb"]=>
string(3) "bbb"
["ccc"]=>
string(6) "aaabbb"
}
---------------------------------------
static obj with an additional prop: 0.042968988418579
memory: 524920
object(staticSet2)#1000 (3) {
["aaa"]=>
string(3) "aaa"
["bbb"]=>
string(3) "bbb"
["ddd"]=>
string(6) "aaabbb"
}
---------------------------------------
我在这里可能是错的,我没有研究PHP源代码,但我认为就PHP而言,一切都是数组。预定义的对象只是定义良好的数组。有趣的是,值类型没有作用,即。字符串值“ 1”将占用与布尔值true相同的内存量。
为什么?就像我之前在PHP中所说的那样,数组是数组,因此,如果您有一个带有setter函数的对象,那么PHP(简化版)的作用是:
$objects = ['objectName' => [
'aaa' => '',
'bbb' => '',
'ccc' => '',
'setValue:f' => function('name', 'value') { ... }
]
];
每次执行时,您都会在功能键中进行查找,该功能键将在查找道具名称时进行查找,并在后台对道具类型等进行任何验证。预定义对象中的内存节省来自于定义一个设置长度的数组(否则在PHP中是不可能的),而不是分配了一块内存。
结论:
静态对象:是的!我们节省内存!但是,如果您尝试在定义之外添加任何内容,则它将变得更大,然后是stdClass。
['aaa'] into [['aaa'],['bbb']]
数组:如果您使用PHP编写,则最好还是喜欢它们,即使字符串也是PHP中的字符数组。
对象:对象具有很酷的功能,它们是使代码井井有条,干净,模块化的绝妙方法。但是众所周知,“力量巨大……”。
最后,我的测试代码和'*'。人们似乎忘记了,如果在脚本运行时不清理变量,它们将保留在内存中,虽然不会影响执行时间(很多),但会影响内存测量。因此,在运行内存分析时,unset()是您的朋友。
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
$noOfObjects = 1000;
$noOfOperations = 1000;
class set {
public function setKey($key, $value) {
$this->{$key} = $value;
}
public function readKey($name) {
return $this->{$name};
}
}
class staticSet {
public string $aaa;
public string $bbb;
public string $ccc;
}
class staticSet2 {
public string $aaa;
public string $bbb;
}
class staticSet3 {
public bool $aaa;
public bool $bbb;
public bool $ccc;
}
$arraysOf=[];
$t0 = microtime(true);
$start=memory_get_usage();
for ($i=0; $i<$noOfObjects; $i++) {
$z = array(); // definition
for ($j=0; $j<$noOfOperations; $j++) {
$z['aaa'] = 'aaa';
$z['bbb'] = 'bbb';
$z['ccc'] = $z['aaa'].$z['bbb'];
}
$arraysOf[]=$z;
}
$end=memory_get_usage();
echo 'arrays: '.(microtime(true) - $t0).PHP_EOL;
echo 'memory: '.($end-$start).PHP_EOL;
var_dump($z);
unset($t0,$start,$end,$z,$arraysOf);
echo '---------------------------------------'.PHP_EOL;
$arraysOf=[];
$t0 = microtime(true);
$start=memory_get_usage();
for ($i=0; $i<$noOfObjects; $i++) {
$z = new set(); // definition
for ($j=0; $j<$noOfOperations; $j++) {
$z->setKey('aaa','aaa');
$z->setKey('bbb','bbb');
$z->setKey('ccc',$z->readKey('aaa').$z->readKey('bbb'));
}
$arraysOf[]=$z;
}
$end=memory_get_usage();
echo 'dynamic obj: '.(microtime(true) - $t0).PHP_EOL;
echo 'memory: '.($end-$start).PHP_EOL;
var_dump($z);
unset($t0,$start,$end,$z,$arraysOf);
echo '---------------------------------------'.PHP_EOL;
$arraysOf=[];
$t0 = microtime(true);
$start=memory_get_usage();
for ($i=0; $i<$noOfObjects; $i++) {
$z = new staticSet(); // definition
for ($j=0; $j<$noOfOperations; $j++) {
$z->{'aaa'} = 'aaa';
$z->{'bbb'} = 'bbb';
$z->{'ccc'} = $z->{'aaa'}.$z->{'bbb'};
}
$arraysOf[]=$z;
}
$end=memory_get_usage();
echo 'static obj: '.(microtime(true) - $t0).PHP_EOL;
echo 'memory: '.($end-$start).PHP_EOL;
var_dump($z);
unset($t0,$start,$end,$z,$arraysOf);
echo '---------------------------------------'.PHP_EOL;
$arraysOf=[];
$t0 = microtime(true);
$start=memory_get_usage();
for ($i=0; $i<$noOfObjects; $i++) {
$z = new staticSet2(); // definition
for ($j=0; $j<$noOfOperations; $j++) {
$z->{'aaa'} = 'aaa';
$z->{'bbb'} = 'bbb';
$z->{'ddd'} = $z->{'aaa'}.$z->{'bbb'};
}
$arraysOf[]=$z;
}
$end=memory_get_usage();
echo 'static obj with an additional prop: '.(microtime(true) - $t0).PHP_EOL;
echo 'memory: '.($end-$start).PHP_EOL;
var_dump($z);
unset($t0,$start,$end,$z,$arraysOf);
echo '---------------------------------------'.PHP_EOL;
$arraysOf=[];
$t0 = microtime($noOfObjects);
$start=memory_get_usage();
for ($i=0; $i<$noOfObjects; $i++) {
$z = new Ds\Map(); // definition
for ($j=0; $j<$noOfOperations; $j++) {
$z->put('aaa','aaa');
$z->put('bbb','bbb');
$z->put('ccc',$z->get('aaa').$z->get('bbb'));
}
$arraysOf[]=$z;
}
$end=memory_get_usage();
echo 'Ds\Map: '.(microtime(true) - $t0).PHP_EOL;
echo 'memory: '.($end-$start).PHP_EOL;
var_dump($z);
unset($t0,$start,$end,$z,$arraysOf);
echo '---------------------------------------'.PHP_EOL;
$arraysOf=[];
$t0 = microtime(true);
$start=memory_get_usage();
for ($i=0; $i<$noOfObjects; $i++) {
$z = new staticSet(); // definition
for ($j=0; $j<$noOfOperations; $j++) {
$z->{'aaa'} = '1';
$z->{'bbb'} = '1';
$z->{'ccc'} = ($z->{'aaa'} && $z->{'bbb'});
}
$arraysOf[]=$z;
}
$end=memory_get_usage();
echo 'static obj with bool values: '.(microtime(true) - $t0).PHP_EOL;
echo 'memory: '.($end-$start).PHP_EOL;
var_dump($z);
unset($t0,$start,$end,$z,$arraysOf);
echo '---------------------------------------'.PHP_EOL;
$arraysOf=[];
$t0 = microtime(true);
$start=memory_get_usage();
for ($i=0; $i<$noOfObjects; $i++) {
$z = new staticSet3(); // definition
for ($j=0; $j<$noOfOperations; $j++) {
$z->{'aaa'} = '1';
$z->{'bbb'} = '1';
$z->{'ccc'} = ($z->{'aaa'} && $z->{'bbb'});
}
$arraysOf[]=$z;
}
$end=memory_get_usage();
echo 'static obj with defined bool values: '.(microtime(true) - $t0).PHP_EOL;
echo 'memory: '.($end-$start).PHP_EOL;
var_dump($z);
unset($t0,$start,$end,$z,$arraysOf);
echo '---------------------------------------'.PHP_EOL;
$arraysOf=[];
$t0 = microtime(true);
$start=memory_get_usage();
for ($i=0; $i<$noOfObjects; $i++) {
$z = array('aaa' => '','bbb' => '','ccc' => ''); // definition
for ($j=0; $j<$noOfOperations; $j++) {
$z['aaa'] = 'aaa';
$z['bbb'] = 'bbb';
$z['ccc'] = $z['aaa'].$z['bbb'];
}
$arraysOf[]=$z;
}
$end=memory_get_usage();
echo 'predefined array: '.(microtime(true) - $t0).PHP_EOL;
echo 'memory: '.($end-$start).PHP_EOL;
var_dump($z);
unset($t0,$start,$end,$z,$arraysOf);
echo '---------------------------------------'.PHP_EOL;
echo 'phpversion '.phpversion().PHP_EOL;