Re: Line numbering an array
- From: "rf" <rf@xxxxxxxxxxx>
- Date: Wed, 10 Dec 2008 09:12:53 GMT
"J.O. Aho" <user@xxxxxxxxxxx> wrote in message
news:6q966oFbdissU1@xxxxxxxxxxxxxxxxxxxxx
Samuel van Laere wrote:
Nothing to do with the OP at all, but the code below
$i = 0;
while ($i <= count($landen)) {
print "$landen[i]<br>";
$i++;
}
Don't use count() in a while condition, nor in a for condition, as your
array
don't change size, which makes it just waste of CPU cycles to
recalculating
the value for each loop, do instead:
I see this advice all over the place and I wonder where it originated from.
Some bad textbook probably.
Without looking at the PHP source code I don't know for sure but I suggest
that count() is merely accessing a "property" of the array which is set
*when* the array changes size, not "calculated" on each call. In any case I
knocked up a test.
Allocate and initialise a one million (yes, 1,000,000) element array, $a,
and then play with it, noting the execution time for various things. BTW
this is on a rather elderly 3GHz computer with WAMP.
for ($i = 0;$i < 1000000;$i++)
{
$a[] = $i;
}
elapsed time 1.46 seconds. Not too shabby.
$c = count($a);
for ($i = 0;$i < $c; $i++)
{
}
Elapsed time .407 seconds.
for ($i = 0;$i < count($a); $i++)
{
}
Elapsed time .926 seconds.
Crikey, over twice as much. But, that .510 second increment is for a loop
iterating 1000000 times. So using count() costs us .510 microseconds per
each.
Also keep in mind that there is absolutely nothing inside that loop. Stuff a
bunch of real statements in there and see what happens. We might go from,
say, 100.11 seconds to 100.62 seconds. Yes, big numbers but that is still
only 100 microseconds, a mere tenth of a millisecond, per iteration We could
to 244 of them without the viewer even noticing. In any case a 1,000,000
element array is not real world.
Lets consider a real world case, that of the OP. The OP has two hundred and
forty four elements in his array. The saving in CPU time by avoiding the
count() function inside the loop is, er, 124 microseconds, on my computer.
How does the OP's array come about? Probably read from a disk file or a
database (which ends up accessing a disk file anyway). Tens of milliseconds
just to get the array. And what other processing does the OPs code do? Well,
he uses a print statement to fiddle with some strings and do some string
concatenation. Probably much longer than .5 microseconds per each.
Oh, I forgot, the server has to find and load the PHP file and then PHP
itself has to parse it into its internal whatever it uses. Also way longer
than 124 microseconds.
What other time is associated with accessing this code from a web browser.
Well, there is the client to server TCP/IP latency. At least 10 milliseconds
if you are across the road. Probably more like 50ms or so. From where I am
to the OP's host over in the Netherlands it's a guaranteed minimum of 300
milliseconds.
Our saving of 124 microseconds starts to pale to insignificance.
Here, however, is a worse scenario:
$c = count($a1);
// do a couple of things
for ($i = 0; $i < $c; $i++)
{
// do something with $a1[$c]
}
for ($i = 0; $i < $c; $i++)
{
// try to do something with $a2[$c]
}
Yep, I copy/pasted the last block of code and forgot to also copy the $c =
bit, because I was concentrating on the for block and what it does. $c still
contains the number of elements in $a1, but my code is blithely accessing
$a2.
Either my code goes bang (good) or silently gives me the wrong results
(bad).
I would rather waste those 124 microseconds and have my for block totally
self contained than to spend an hour or five trying to find why my shopping
cart does not deliver all my viewers purchases.
I consider this to be a perfect example of premature optimisation.
FWIW I saw a similar thing just the other day over in the Javascript group.
.
- Follow-Ups:
- Re: Line numbering an array
- From: Thomas Mlynarczyk
- Re: Line numbering an array
- From: J.O. Aho
- Re: Line numbering an array
- From: rf
- Re: Line numbering an array
- References:
- Line numbering an array
- From: Samuel van Laere
- Re: Line numbering an array
- From: Tim Greer
- Re: Line numbering an array
- From: Samuel van Laere
- Re: Line numbering an array
- From: J.O. Aho
- Line numbering an array
- Prev by Date: Re: Line numbering an array
- Next by Date: Re: Line numbering an array
- Previous by thread: Re: Line numbering an array
- Next by thread: Re: Line numbering an array
- Index(es):
Relevant Pages
|