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($key s);
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 4 2206
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($key s);
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!
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=a rray();
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($key s);
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
*** 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
--
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($key s);
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!) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: csx |
last post by:
Hi everyone!
two quick questions relating to arrays.
Q1,
Is it possible to re-assign array elements?
int array = {{2,4}, {4,5}};
array = {2,3}
|
by: Kin Pang |
last post by:
Hi,
I have a routine where I'm using a std::map from a pair of ints to double
for caching evaluation results.
The cache depth could be upto say 1000 in depth.
However, my application needs to clear and replenish the cache repeatedly
and profiling seems to suggests
the performance is handicated by repeated calls to new.
Can anybody advice me on how to improve the performance or suggests
alternatives please.
|
by: Christopher Benson-Manica |
last post by:
Yesterday I changed some code to use std::vectors and std::strings
instead of character arrays. My boss asked me today why I did it, and
I said that the code looks cleaner this way. He countered by saying
that he was regarded the dyanamic allocation that C++ STL classes
perform as being very inefficient. He also said that he wasn't
particularly interested in clean code (!). My question to the group:
In what situations, if any, would...
|
by: aaronfude |
last post by:
I'm working on a scientific computing application. I need a class
called Element which is no more than a collection of integers, or
"nodes" and has only on method int getNode(int i).
I would like to implement in the most efficient was possible. So I
summoned up my programming intellect and asked myself: Do I want to
have members such as int a, b, c, d, e or a single member such as int
a. So I wrote the following snippet and compiled it...
|
by: mark |
last post by:
Hello-
i am trying to make the function addbitwise more efficient. the code
below takes an array of binary numbers (of size 5) and performs
bitwise addition. it looks ugly and it is not elegant but it appears
to work. using time, i measured it takes .041s to execute, which i
admit isnt much. but, the problem is that this procedure will be
called many, many times in my project (probably at least a few
thousand times, if not more) so...
| |
by: Mike Cain |
last post by:
I have an odd situation I'm trying to understand..... I'm using MS VS 7.0
C++ with the MFC CBuffer class.
If I do this:
#define NUM_ELEMENTS 2
CBuffer<char, 512 x;
CBuffer<char, 512 y;
So as you can see I have two variables which will hold strings, each of
|
by: anon.asdf |
last post by:
In terms of efficieny:
Is it better to use multiple putchar()'s after one another as one gets
to new char's
OR
is it better to collect the characters to a char-array first, and then
use puts() to print to screen
????
|
by: Lars |
last post by:
I need to multiply to large matrices (of type double), where one is
"dense" (mostly non-zero entries) and the other is "sparse" (mostly
zero entries).
Will it be more efficient to check for non-zero entries, or does it
matter at all?
If B is "sparse":
for (i = 0; i <= nr-1; i++) {
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| | |