note 95176 added to function.array-merge
| From: | randallgirard at hotmail dot com | Date: | Wed, 16 Dec 2009 22:16:04 +0000 |
| Subject: | note 95176 added to function.array-merge | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-164057@lists.php.net to get a copy of this message | ||
I am in the middle of recoding a very complex CMS I wrote in the past, and I often used
array_merge() to determine default values (example: paths, keys which refer to objects elsewhere,
etc).
I was trying to decide if I should use array_merge or individual calls to isset in combination with
an $opts array, to supply default values in functions. For example, here would be two simple
examples:
<?php
function foo( array $opts = array() ) {
if ( isset($opts['val1']) ) $opts['val1'] = 'value';
if ( isset($opts['val2']) ) $opts['val2'] = 'value';
if ( isset($opts['val3']) ) $opts['val3'] = 'value';
# ... continue coding
}
function bar( array $opts = array() ) {
$opts = array_merge( array(
'val1' => 'value',
'val2' => 'value',
'val3' => 'value',
), $opts);
# ... continue coding
}
?>
Hypothesis: isset() will be dramatically faster in both circumstances, as benchmarks I have
performed in the past usually demonstrate that array iterations are typically slow and should be
avoided when and where possible. (ex: using strrpos and substr rather than explode and array_pop to
get a file name from a path, of course you can just use basename() but this is just for explanation
purposes)
---
I performed numerous benchmarks using loops, and then hard-coded examples and got fairly similar
results. My four focus points however, were supplying the values in the $opts array, and not
supplying the values, with both methods.
Calls to isset() WITH the values supplied was ALWAYS the fastest (which makes sense, as no
assignments are made)
Calls to isset() WITH the values supplied, and array_merge() with NO values set came very close in
times, and fluctuated which was faster or slower...
(This surprised me as I expected that working with array_merge would simply take more time; But also
makes sense as I think about it more as you are still utilizing an array and array_merge is probably
making isset() calls in itself)
Calls to array_merge() with NO values supplied was always the SLOWEST. (which also makes sense,
because all values are replaced)
---
Conclusion: Calls to isset() are faster than using array_merge(); But not by much. However, the more
values supplied when calling the function determine how much closer the execution time will take.
Therefor, with this type of functionality in mind, supplying DEFAULT values mean often NO, SOME, or
ALL values will be included when calling the function...
Because I am worried about the execution time over easy / less code, I'll stick with the
isset() method of supplying default values to an array.
---
Now that the test results are in, I thought I would share as I spent quite a bit of time testing and
going over results.
----
Server IP: 64.71.164.2
Probable Submitter: 76.167.60.11
----
Manual Page -- http://www.php.net/manual/en/function.array-merge.php
Edit -- https://master.php.net/note/edit/95176
Del: integrated -- https://master.php.net/note/delete/95176/integrated
Del: useless -- https://master.php.net/note/delete/95176/useless
Del: bad code -- https://master.php.net/note/delete/95176/bad+code
Del: spam -- https://master.php.net/note/delete/95176/spam
Del: non-english -- https://master.php.net/note/delete/95176/non-english
Del: in docs -- https://master.php.net/note/delete/95176/in+docs
Del: other reasons-- https://master.php.net/note/delete/95176
Reject -- https://master.php.net/note/reject/95176
Search -- https://master.php.net/manage/user-notes.php