note 116943 added to function.array-push
| From: | golddragon007 | Date: | Wed, 25 Mar 2015 10:51:20 +0000 |
| Subject: | note 116943 added to function.array-push | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-202029@lists.php.net to get a copy of this message | ||
I did a performance check, and I saw, if you push more than one value it can be faster the array
push, that the normal $array[] version.
Case 1: $array[] = something;
Case 2: array_push($array, $value);
Case 3: array_push($array, $value1, $value2, $value3 [...]); $values are definied
Case 4: array_push($array, $value1, $value2, $value3 [...]); $values are definied, when $array is
not empty
Case 5: Case1 + Case 3
Case 6: Result array contains some value (Case 4)
Case 7: Result array contains same value as the push array (Case 4)
-----------------------------------------------------------------------------------------------------------
~~~~~~~~~~~~ Case 1 ~~~~~~~~~~~~
Times: 0.0310 0.0300 0.0290 0.0340 0.0400 0.0440 0.0480 0.0550 0.0570 0.0570
Min: 0.0290
Max: 0.0570
Avg: 0.0425
~~~~~~~~~~~~ Case 2 ~~~~~~~~~~~~
Times: 0.3890 0.3850 0.3770 0.4110 0.4020 0.3980 0.4020 0.4060 0.4130 0.4200
Min: 0.3770
Max: 0.4200
Avg: 0.4003
~~~~~~~~~~~~ Case 3 ~~~~~~~~~~~~
Times: 0.0200 0.0220 0.0240 0.0340 0.0360 0.0410 0.0460 0.0500 0.0520 0.0520
Min: 0.0200
Max: 0.0520
Avg: 0.0377
~~~~~~~~~~~~ Case 4 ~~~~~~~~~~~~
Times: 0.0200 0.0250 0.0230 0.0260 0.0330 0.0390 0.0460 0.0510 0.0520 0.0520
Min: 0.0200
Max: 0.0520
Avg: 0.0367
~~~~~~~~~~~~ Case 5 ~~~~~~~~~~~~
Times: 0.0260 0.0250 0.0370 0.0360 0.0390 0.0440 0.0510 0.0520 0.0530 0.0560
Min: 0.0250
Max: 0.0560
Avg: 0.0419
~~~~~~~~~~~~ Case 6 ~~~~~~~~~~~~
Times: 0.0340 0.0280 0.0370 0.0410 0.0450 0.0480 0.0560 0.0580 0.0580 0.0570
Min: 0.0280
Max: 0.0580
Avg: 0.0462
~~~~~~~~~~~~ Case 7 ~~~~~~~~~~~~
Times: 0.0290 0.0270 0.0350 0.0410 0.0430 0.0470 0.0540 0.0540 0.0550 0.0550
Min: 0.0270
Max: 0.0550
Avg: 0.044
Tester code:
// Case 1
$startTime = microtime(true);
$array = array();
for ($x = 1; $x <= 100000; $x++)
{
$array[] = $x;
}
$endTime = microtime(true);
// Case 2
$startTime = microtime(true);
$array = array();
for ($x = 1; $x <= 100000; $x++)
{
array_push($array, $x);
}
$endTime = microtime(true);
// Case 3
$result = array();
$array2 = array(&$result)+$array;
$startTime = microtime(true);
call_user_func_array("array_push", $array2);
$endTime = microtime(true);
// Case 4
$result = array();
for ($x = 1; $x <= 100000; $x++)
{
$result[] = $x;
}
$array2 = array(&$result)+$array;
$startTime = microtime(true);
call_user_func_array("array_push", $array2);
$endTime = microtime(true);
// Case 5
$result = array();
$startTime = microtime(true);
$array = array(&$result);
for ($x = 1; $x <= 100000; $x++)
{
$array[] = $x;
}
$endTime = microtime(true);
// Case 6
$result = array(1,2,3,4,5,6);
$startTime = microtime(true);
$array = array(&$result);
for ($x = 1; $x <= 100000; $x++)
{
$array[] = $x;
}
$endTime = microtime(true);
// Case 7
$result = array();
for ($x = 1; $x <= 100000; $x++)
{
$result[] = $x;
}
$startTime = microtime(true);
$array = array(&$result);
for ($x = 1; $x <= 100000; $x++)
{
$array[] = $x;
}
$endTime = microtime(true);
----
Server IP: 72.52.91.14
Probable Submitter: 89.147.72.60
----
Manual Page -- http://php.net/manual/en/function.array-push.php
Edit -- https://master.php.net/note/edit/116943
Del: integrated -- https://master.php.net/note/delete/116943/integrated
Del: useless -- https://master.php.net/note/delete/116943/useless
Del: bad code -- https://master.php.net/note/delete/116943/bad+code
Del: spam -- https://master.php.net/note/delete/116943/spam
Del: non-english -- https://master.php.net/note/delete/116943/non-english
Del: in docs -- https://master.php.net/note/delete/116943/in+docs
Del: other reasons-- https://master.php.net/note/delete/116943
Reject -- https://master.php.net/note/reject/116943
Search -- https://master.php.net/manage/user-notes.php