Re: Line numbering an array




"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.


.



Relevant Pages

  • Re: time in us in OnTimer
    ... Windows is completely inappropriate for this purpose. ... you cannot use a general-purpose operating system. ... accuracy of more than a few hundred milliseconds under normal conditions, ... I have, next to my desk, a mass spectrometer that samples every 27 microseconds. ...
    (microsoft.public.vc.mfc)
  • Re: time in us in OnTimer
    ... you cannot use a general-purpose operating system. ... accuracy of more than a few hundred milliseconds under normal conditions, ... microseconds are ... It is handled by internal timing loops in the embedded processor. ...
    (microsoft.public.vc.mfc)
  • Re: time in us in OnTimer
    ... It isn't going to happen in Windows. ... you cannot use a general-purpose operating system. ... accuracy of more than a few hundred milliseconds under normal conditions, ... microseconds are ...
    (microsoft.public.vc.mfc)
  • Re: Does C# have a built-in way to format numbers like this?
    ... that will cost you 140 microseconds. ... Even though the while loop may have bothered you ... difference of 8 seconds between both methods and when passing a 19 ... Faster by 3 seconds when passing a single digit ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Better mehod to generate a sequence of range?
    ... I would go so far as to say that since loop is part of the standard, if an implementation can't produce fast code from a loop form, it is a defective implementaion. ... During that period, 414,414 microseconds were spent in user mode ... 5,357 minor page faults, 19,397 major page faults, 1 swaps. ...
    (comp.lang.lisp)