View Full Version : Use of succ() and pred() functions
FusionWolf
20-01-2005, 09:55 PM
Hi,
Is that common to use succ() and pred() functions when doing simple math (adding and subtracting values)?
For example for setting size of dynamic variable.
SetLength(V, Succ(Length(V)));
Versus this...
SetLength(V, Length(V) + 1);
Which one is faster?
Succ() and Pred() would usually be used for enumerated values.
type
TSampleEnum = (seOne, seTwo, seThree);
var
Enum: TSampleEnum;
begin
Enum := seTwo;
Enum := Succ(Enum);
end;
For the case you describe, Length(V) + 1 is definitely the more common usage.
As an aside, I read somewhere (I think it was on High Performance Delphi which is now gone), that
i := i + 1;
is faster than
Inc(i);
Mind you, I have not given this any serious testing, so it's just hearsay at the moment.
Paulius
20-01-2005, 10:29 PM
As an aside, I read somewhere (I think it was on High Performance Delphi which is now gone), that
i := i + 1;
is faster than
Inc(i);
Not quite true, add operation could be faster than inc as inc cannot be pipelined, but doing a check in Delphi6 reveales that all these
i := i + 1;
Inc(i);
i:= succ(i);
generate the same code - inc.
FusionWolf
20-01-2005, 10:43 PM
Okey, thanks for your answers. It seems to that I have to change my way to do things.
That's not to say that your usage is wrong. It is still a quite valid and legal way to do things.
FusionWolf
20-01-2005, 11:18 PM
That's not to say that your usage is wrong. It is still a quite valid and legal way to do things.
Yeah sure, but I want to maximize the performance of my code (I'm programming a game with DX right now).
As Paulius said, all three approaches produced the same assembly code, so they are all the same speed.
FusionWolf
21-01-2005, 12:10 AM
As Paulius said, all three approaches produced the same assembly code, so they are all the same speed.
Ouh. Then it doesn't matter how I code it.
marcov
27-01-2005, 11:49 AM
As Paulius said, all three approaches produced the same assembly code, so they are all the same speed.
IIRC on Turbo Pascal succ() and pred() were faster, just like with TP
x:=x+1;
was slower than inc(x);
So people using succ and pred instead of +1 and -1 probably have this habit from TP times.
FPC and Delphi optimize this away, and succ and pred are now typically only useful for enumerations.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.