Is there any performance difference between this two ones ?
$rules['a']['x'] = 'someValue';
$rules['a']['y'] = 'anotherValue';
and
$rules['a'] = [
'x' => 'someValue',
'y' => 'anotherValue',
];
Is there any performance difference between this two ones ?
$rules['a']['x'] = 'someValue';
$rules['a']['y'] = 'anotherValue';
and
$rules['a'] = [
'x' => 'someValue',
'y' => 'anotherValue',
];
As noted by everybody else, any difference would be neglible and you shouldn't care about this.
That being said though, the second method should technically be more performant, because (at least it looks like) it does the entire thing in a single instruction.
When you do this:
$rules['a']['x'] = 'someValue';
$rules['a']['y'] = 'anotherValue';
... the PHP engine has to first check whether $rules
exists, whether it's an array, an ArrayAccess
object or not (and error in that case). After that, it has to do the same for $rules['a']
.
And it has to do that twice because both lines represent separate expressions.
While on the other hand:
$rules['a'] = [
'x' => 'someValue',
'y' => 'anotherValue',
];
... will only check what $rules
is (not caring whether $rules['a']
exists or what type it is) and it only has to do that once.
Note that this is a very, very simplified explanation and there are a lot of other factors that come into play and make a difference. But that's the benefit of using a high-level language like PHP - in general, you don't have to care or even know how things work behind the scene.
If you want to make performance optimizations, this is certainly the wrong thing to look at. Find where your "bottlenecks" are and work on them (pro tip: 9 times out of 10, it's your database queries).