Re: I need the fastest routine
- From: "Rudy Velthuis [TeamB]" <newsgroups@xxxxxxxxxxxx>
- Date: Sat, 5 Jul 2008 19:48:39 +0200
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
.
- Follow-Ups:
- Re: I need the fastest routine
- From: Q Correll
- Re: I need the fastest routine
- From: Rudy Velthuis [TeamB]
- Re: I need the fastest routine
- References:
- I need the fastest routine
- From: Clément Doss
- Re: I need the fastest routine
- From: Q Correll
- Re: I need the fastest routine
- From: Clément Doss
- Re: I need the fastest routine
- From: Q Correll
- Re: I need the fastest routine
- From: Stig Johansen
- I need the fastest routine
- Prev by Date: Re: I need the fastest routine
- Next by Date: Re: I need the fastest routine
- Previous by thread: Re: I need the fastest routine
- Next by thread: Re: I need the fastest routine
- Index(es):
Relevant Pages
|