Re: I need the fastest routine



Stig Johansen wrote:

Since this apperently is performance critical, he could also just use
a pointer, something like this:
procedure MinMaxArray( const aArray : Array of Integer; out aMax,
aMin : integer );
var i : integer;
var p : pinteger ;
begin
aMax := minInt;
aMin := maxInt;
p := @aArray[0] ;
for i := 0 to high(aArray) do
begin
if p^ > aMax then aMax := p^ ;
if p^ < aMin then aMin := p^;
inc(p);
end;
end;

Then he can avoid the overhead of function calls with the min and max
functions.

I didn't check, but I'm pretty sure that code that does not use
pointers will be equally fast (but IMO, more readable, and also more
portable), since the optimizer will turn it into the code that you
posted anyway:

procedure MinMaxArray(const aArray: array of Integer;
out aMax, aMin: Integer);
var
I: Integer;
begin
aMax := MinInt;
aMin := MaxInt;
for I := 0 to High(aArray) do
begin
if aArray[I] > aMax then
aMax := aArray[I];
if aArray[I] < aMin then
aMin := aArray[I];
end;
end;

I'm not sure if this is faster (I could imagine the optimizer already
does this), but it could be:

procedure MinMaxArray(const aArray: array of Integer;
out aMax, aMin: Integer);
var
I: Integer;
Value: Integer;
begin
aMax := MinInt;
aMin := MaxInt;
for I := 0 to High(aArray) do
begin
Value := aArray[I];
if Value > aMax then
aMax := Value;
if Value < aMin then
aMin := Value;
end;
end;

--
Rudy Velthuis [TeamB] http://www.teamb.com

"Mit der Dummheit kämpfen Götter selbst vergebens"
"Against stupidity the (very) gods themselves contend in vain"
-- Friedrich von Schiller
.



Relevant Pages

  • Re: I need the fastest routine
    ... procedure MinMaxArray(const aArray: Array of Integer; out aMax, aMin: ... var i: integer; ...
    (borland.public.delphi.language.basm)
  • Re: Anyone recognise this sort algorithm?
    ... if A> AMax then AMax = A ... if A< Amin then Amin = A ... Counting sort runs in O, if the range is a known constant. ... Well spotted Arthur, I was convinced this type of sort had been ...
    (comp.programming)
  • Re: Anyone recognise this sort algorithm?
    ... I dont have the dos bat code to hand but here is what I mean by no ... if A> AMax then AMax = A ... if A< Amin then Amin = A ... @echo off ...
    (comp.programming)
  • Re: I need the fastest routine
    ... Rudy Velthuis [TeamB] wrote: ... if aArray> aMax then ... if aArray< aMin then ... FOUR pebbles. ...
    (borland.public.delphi.language.basm)
  • Re: I need the fastest routine
    ... procedure MinMaxArray(const aArray: Array of Integer; out aMax, aMin: ... i, m, mmod, n: Integer; ...
    (borland.public.delphi.language.basm)