Results 1 to 9 of 9

Thread: Use of succ() and pred() functions

  1. #1

    Use of succ() and pred() functions

    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.

    Code:
      SetLength(V, Succ(Length(V)));
    Versus this...

    Code:
      SetLength(V, Length(V) + 1);
    Which one is faster?
    - In a world without the wall and fences, who will need the Gates and Windows.

  2. #2

    Use of succ() and pred() functions

    Succ() and Pred() would usually be used for enumerated values.

    [pascal]
    type
    TSampleEnum = (seOne, seTwo, seThree);

    var
    Enum: TSampleEnum;
    begin
    Enum := seTwo;
    Enum := Succ(Enum);
    end;
    [/pascal]
    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
    [pascal]
    i := i + 1;
    [/pascal]
    is faster than
    [pascal]
    Inc(i);
    [/pascal]
    Mind you, I have not given this any serious testing, so it's just hearsay at the moment.

  3. #3

    Use of succ() and pred() functions

    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.

  4. #4

    Use of succ() and pred() functions

    Okey, thanks for your answers. It seems to that I have to change my way to do things.
    - In a world without the wall and fences, who will need the Gates and Windows.

  5. #5

    Use of succ() and pred() functions

    That's not to say that your usage is wrong. It is still a quite valid and legal way to do things.

  6. #6

    Use of succ() and pred() functions

    Quote Originally Posted by Sly
    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).
    - In a world without the wall and fences, who will need the Gates and Windows.

  7. #7

    Use of succ() and pred() functions

    As Paulius said, all three approaches produced the same assembly code, so they are all the same speed.

  8. #8

    Use of succ() and pred() functions

    Quote Originally Posted by Sly
    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.
    - In a world without the wall and fences, who will need the Gates and Windows.

  9. #9

    Use of succ() and pred() functions

    Quote Originally Posted by Sly
    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •