Article

From:
To:
Philippe Ranger
Subject:
Re: Cardinality of a set
Newsgroup:
borland.public.delphi.objectpascal

Re: Cardinality of a set

Philippe Ranger wrote:
>  
> It looks like you worked on this. Can I ask --

Actually, I didn't 'work' on it. I just applied my standard tune-ups,
then looked at what I got (CPU window).  It looked good so I quit.

> 
> -- Why not a For loop?
For loops usually have more setup overhead and usually increment a
counter and one pointer for each array in the loop.  The while loop has
less overhead (although the fooling around at the beginning could be
considered part of its overhead) and only increments the index. 
However, it also feels compelled to test the counter against zero even
though the inc would have set the flags needed.

> 
> -- Why scan down the array?

I don't.  I scan up referened from the back of the array.  Thus i goes
from -size to -1.  This allows a termination at at constant (zero)
rather than at a variable.  Net effect: reduce number of variables in
play by one.

> 
> -- Why use the var b? Is using this buffercheaper than doing a second
> indexed access to the same spot?

This was a guess.  The idea was to prevent reloading Data[i].  Sometimes
the compiler handles this on its own, sometimes not (it probably would
have here) Anyway, b is free as it will only be a register variable
(optimization on) and you have to load Data[i] sometime.

 
> In other words, what if this was --

In this particular instance this would probably have been quite
similar.  Lets Check.... Yup almost exactly the same.  However, there
are a couple of things in your version that don't generalize well.  One
is the for loop as I mentioned.  Another is the use of 'absolute'. 
Using absolute as a sort of permanent typecast often forces register
variables onto the stack.  In this case, the data is already memory
based so it works out.

My approach to this sort of thing is to start with the probabilities on my side. That is I code in the style that I know tends to result in the most efficient machine code. This way I don't have to go back and completely re-invent the wheel with each optimization.
BTW: I really wish they would 'fix' the for loop because I rather dislike using while to do a for loops job.

-- Bob Lee High Performance Delphi - http://www.econos.com/optimize/
FYI: Phrase searches are enclosed in either single or double quotes
 
 
Originally created by
Tamarack Associates
Tue, 26 Nov 2024 11:53:18 UTC
Copyright © 2009-2024
HREF Tools Corp.