By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,045 Members | 1,305 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,045 IT Pros & Developers. It's quick & easy.

Efficiency question: Overwrite array value, or ask if it is set?

P: n/a
Hi

While looping through an array I extract all keys on the fly, as in this
example:

$data = array(
array('name' ='Helen', 'age' ='40'),
array('name' ='Mark', 'email' ='****@xyz.com', 'age' ='90'),
);
$keys = array();
foreach ($data as $row) {
foreach ($row as $key =$value) {
// do other things here
$keys[$key] = 1;
}
}
$column_names = array_keys($keys);

Now my question is about the line
$keys[$key] = 1;

This overwrites existing entries and thus creates a set of unique keys.
This could also be done without overwriting:
if (!isset($keys[$key])) $keys[$key] = 1;

So I wonder which is more efficient - overwriting the array entry at
every loop, or checking for it with the if statement.

Thanks for a comment
Markus
Jul 6 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Markus Ernst wrote:
Hi

While looping through an array I extract all keys on the fly, as in
this example:

$data = array(
array('name' ='Helen', 'age' ='40'),
array('name' ='Mark', 'email' ='****@xyz.com', 'age' ='90'),
);
$keys = array();
foreach ($data as $row) {
foreach ($row as $key =$value) {
// do other things here
$keys[$key] = 1;
}
}
$column_names = array_keys($keys);

Now my question is about the line
$keys[$key] = 1;

This overwrites existing entries and thus creates a set of unique
keys. This could also be done without overwriting:
if (!isset($keys[$key])) $keys[$key] = 1;

So I wonder which is more efficient - overwriting the array entry at
every loop, or checking for it with the if statement.

Thanks for a comment
Markus
ISTR a general rule that said that assignments were more efficienct that
tests.
If this is not the case, then the answer to your question depends on whether
in most cases you will end up doing the assignment, or whether in most cases
the value is already set.
If the former is true, then it is certainly not more efficient, if the
latter then it may be. This is where knowing something about the data is
required.

I think that all makes sense!
Jul 6 '06 #2

P: n/a
I understand what you are doing. I changed it a little which I believe
is a little more intuitive. It may even be faster.

$data = array(
array('name' ='Helen', 'age' ='40'),
array('name' ='Mark', 'email' ='...@xyz.com', 'age' ='90'),
);
$column_names=array();
foreach ($data as $row) {
foreach ($row as $key =$value) {
// do other things here
if (!in_array($key,$column_names)) $column_names[]=$key;
}
}
Markus Ernst wrote:
Hi

While looping through an array I extract all keys on the fly, as in this
example:

$data = array(
array('name' ='Helen', 'age' ='40'),
array('name' ='Mark', 'email' ='****@xyz.com', 'age' ='90'),
);
$keys = array();
foreach ($data as $row) {
foreach ($row as $key =$value) {
// do other things here
$keys[$key] = 1;
}
}
$column_names = array_keys($keys);

Now my question is about the line
$keys[$key] = 1;

This overwrites existing entries and thus creates a set of unique keys.
This could also be done without overwriting:
if (!isset($keys[$key])) $keys[$key] = 1;

So I wonder which is more efficient - overwriting the array entry at
every loop, or checking for it with the if statement.

Thanks for a comment
Markus
Jul 6 '06 #3

P: n/a
*** Markus Ernst escribió/wrote (Thu, 06 Jul 2006 10:52:50 +0200):
So I wonder which is more efficient - overwriting the array entry at
every loop, or checking for it with the if statement.
A quick dirty benchmark with loops and microtime(), using the code you
posted, shows that assigning is slightly faster that testing, running from
command line. The saving ranges from 3% to 6%.

If you're interested in these issues you may like Xdebug and WinCacheGrind:

http://xdebug.org/
http://sourceforge.net/projects/wincachegrind/

--
-+ http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
++ Mi sitio sobre programación web: http://bits.demogracia.com
+- Mi web de humor con rayos UVA: http://www.demogracia.com
--
Jul 6 '06 #4

P: n/a
Markus Ernst schrieb:
While looping through an array I extract all keys on the fly, as in this
example:

$data = array(
array('name' ='Helen', 'age' ='40'),
array('name' ='Mark', 'email' ='****@xyz.com', 'age' ='90'),
);
$keys = array();
foreach ($data as $row) {
foreach ($row as $key =$value) {
// do other things here
$keys[$key] = 1;
}
}
$column_names = array_keys($keys);

Now my question is about the line
$keys[$key] = 1;

This overwrites existing entries and thus creates a set of unique keys.
This could also be done without overwriting:
if (!isset($keys[$key])) $keys[$key] = 1;

So I wonder which is more efficient - overwriting the array entry at
every loop, or checking for it with the if statement.
Thank you all for your inputs! I finally tested the versions suggested
with my real data.
Version 1: $keys[$key] = $key;
Version 2: if (!isset($keys[$key])) $keys[$key] = $key;
Version 3: if (!in_array($key, $keys)) $keys[] = $key;

Though it was only a few lines, all versions in the same script, the
rankings differed from call to call; maybe I could say that version 1
tends to be fastest when total execution is fast, and version 3 tends to
win when total execution is slower.

Anyway the range was between 0.05 and 0.1 second, which was not really
the reason for my total execution time of 25-35 seconds! (Going on
measuring and optimizing other parts of the code, I reduced it to 3-5
seconds now!)
Jul 7 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.