Re: wich is faster

From: Dave (recneps.w.divad_at_elcaro.moc)
Date: 05/24/04


Date: Mon, 24 May 2004 12:48:50 +0100


buda wrote:
> Wich is (generally/usually) faster: a comparison, or an assignment?
> For example the following pseudo:
>
> flag <- 0
> loop (condition)
> do something
> flag <-1
> endloop
>
> or
>
> flag <- 0
> loop(condition)
> do something
> if (flag = 0)
> flag <- 1
> endif
> endloop
>
>

Nobody seems to have mentioned it, so...

The second will be slower because you're doing a comparison AND and
assignment (because flag=0 is true). Change the comparison to flag<>0
and you have a more realistic test.

Also it depends on the processor. Does it have a zero-register command
(like xor a,a on the z80), or does it have to load a literal 0 from the
code (ld a,0)? Same for the test - does it have some way of setting
flags with a single byte instruction (and a), or does it have to compare
the register with zero then deal with the result of the subtraction (cp
a,0)?

Having said that, flag=0; if (flag==0) flag=1; is pointless - flag is
always going to be 1 at the end so you may as well skip the test and
just assign 1.

On the premature optimisation front - it has to be a judgement call by
the programmer. Optimising something you don't know to be slow should
not be done until you know (a) it is slow and (b) it is a problem. If
you know two algorithms for something, one is faster but slightly less
readable than the other, I'd probably go for the faster one depending on
how much less readable it is. Trivial example - testing for primeness
of n could involve an array of size n(+1 because subtracting 1 from
everything is "unreadable") and a Sieve of Eratosthenes function. That
is going to be substantially slower and much more memory intensive than
an "& 1" and a simple loop dividing n by odd numbers from 3 to sqrt(n).
  Depending on the mathematical knowledge of one's peers, the latter may
be unknown and is thus "unreadable", so should the sieve be used because
of that? I think not. I do not accept that readability is an excuse
for deliberately writing crap code, but that there is a balance that
must be reached. I would propose a similar rule to "premature
optimisation is the root of all kinds of evil", namely "excessive
program simplification is also the root of all kinds of evil", including
such crap as "#define BEGIN {", #define END_OF_LINE ;" and so on.

Dave.



Relevant Pages

  • Re: C# Command to Wait a Specified Period of Time
    ... But the key to use the query syntax is you don't do the for each loop yourself, instead you state what you want, not how to do it. ... I'm sure that once one gains experience with these newer language constructs that they do enhance "readability" - for the people who know and use them. ... But don't go around saying that a code block consisting of delegates and anonymous methods is "more readable" than a simple "for loop". ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Larkin, Power BASIC cannot be THAT good:
    ... Using DDR2 ram execution is roughly 2s and older DDR ram is about 3s in execution. ... A cache aware version of this vector add and accumulate is about 15% faster than the simple loop on a good optimising compiler (actually that is measured on MSC I haven't checked its code generation for optimisation - too tedious). ...
    (sci.electronics.design)
  • Re: std::vector : begin, end and insert - Using Objects instead of ints
    ... When there's a choice, prefer preincrement to postincrement, because pre ... The thing is, efficiency can be measured objectively, while "readability" ... split the operation into two statements, e.g. when erasing a list iterator. ... certain kind of loop that omits stmt3 in a for-loop header when you decide ...
    (microsoft.public.vc.mfc)
  • Re: Letter to US Sen. Byron Dorgan re unpaid overtime
    ... it's a for loop in the C sense. ... > sloppy thinking that results from confusing a programming language ... >> I do not believe that you are capable of writing a conforming C compiler. ... Does Microsoft's C compiler perform this optimisation? ...
    (comp.programming)
  • Re: OT. Good programming techniques (or not)...
    ... It's very easy to turn an Oroutine ... not perform that invariant code optimisation automatically because it ... this is one reason FORTRAN generates faster ... The code has manual loop ...
    (uk.comp.sys.mac)