Issues

ZF-5431: using "$arr[] = $var" instead of "array_push($arr, $var)"

Issue Type: Sub-task Created: 2009-01-08T09:52:59.000+0000 Last Updated: 2009-01-29T10:33:49.000+0000 Status: Closed Fix version(s): Reporter: Marc Bennewitz (GIATA mbH) (mben) Assignee: Ralph Schindler (ralph) Tags: Related issues: Attachments:

Description

Comments

Posted by Marc Bennewitz (GIATA mbH) (mben) on 2009-01-08T09:54:59.000+0000

bechmark:

<pre class="highlight">
echo '"array_push($arr, $var)" vs. "$arr[] = $var" (add 1 var)'."\n";
$max = 100000;
$var = true;

echo ' array_push($arr, $var): ';
$arr = array();
$start = microtime(true);
for ($i=0; $i<$max; $i++) {
    array_push($arr, $var);
}
$end = microtime(true);
echo ($end-$start)."\n";

echo ' $arr[] = $var         : ';
$arr = array();
$start = microtime(true);
for ($i=0; $i<$max; $i++) {
    $arr[] = $var;
}
$end = microtime(true);
echo ($end-$start)."\n";
echo "--------------------------------\n";



echo '"array_push($arr, $var)" vs. "$arr[] = $var" (add 10 vars)'."\n";
$max = 100000;
$var = true;

echo ' array_push($arr, $var): ';
$arr = array();
$start = microtime(true);
for ($i=0; $i<$max; $i++) {
    array_push($arr, $var, $var, $var, $var, $var, $var, $var, $var, $var, $var);
}
$end = microtime(true);
echo ($end-$start)."\n";

echo ' $arr[] = $var         : ';
$arr = array();
$start = microtime(true);
for ($i=0; $i<$max; $i++) {
    $arr[] = $var; $arr[] = $var; $arr[] = $var; $arr[] = $var; $arr[] = $var;
    $arr[] = $var; $arr[] = $var; $arr[] = $var; $arr[] = $var; $arr[] = $var;
}
$end = microtime(true);
echo ($end-$start)."\n";

output:

<pre class="highlight">
"array_push($arr, $var)" vs. "$arr[] = $var" (add 1 var)
 array_push($arr, $var): 0.0417320728302
 $arr[] = $var         : 0.021378993988
--------------------------------
"array_push($arr, $var)" vs. "$arr[] = $var" (add 10 vars)
 array_push($arr, $var): 0.147557020187
 $arr[] = $var         : 0.155264139175

Posted by Ralph Schindler (ralph) on 2009-01-10T09:38:10.000+0000

Whats the issue here? The usage of one or the other has always been a negligible difference. Please add more commentary as to what you are advocating.

Posted by Marc Bennewitz (GIATA mbH) (mben) on 2009-01-29T01:45:42.000+0000

to add one element to an array the function is twice as slowly as the operator.

Posted by Ralph Schindler (ralph) on 2009-01-29T10:33:40.000+0000

This is not a performance issue. The performance difference between [] and array_push is negligible at best and both methods are valid for different reasons.

Bracket notation is useful for adding values to a variable (whether it has been initialized as an array or not). $i = null; $i[] = 'foo'; will not throw a notice, where as array_push($i, 'foo') will throw a E_WARN that the first argument must be an array.

Also, array_push() will return the count of all the elements which in some situations might be useful. Ultimately, its up to the component developer which method better suites the needs of the component.

Have you found an issue?

See the Overview section for more details.

Copyright

© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts