Page 1 of 4 123 ... LastLast
Results 1 to 10 of 31

Thread: "is" versus "<> nil" on speed?

  1. #1

    "is" versus "<> nil" on speed?

    I have several loops and brances in which I'm using "<> nil" to check if the object reference is valid or an end of linked list have been found. But, I know "is" works in the same way, and at the same time is makes a type check. I'm thinking on changing all my "<> nil" comparisons by "is". Is it a good idea, or does the operation spends more processor cycles??

    By the way, Happy New Year to the PGD staff and members, just in case I don't post anything later

  2. #2

    Re: "is" versus "<> nil" on speed?

    [quote="cronodragon"]I have several loops and brances in which I'm using "<> nil" to check if the object reference is valid or an end of ]

    i'd say they are most likely both branched, so you should make a speedtest..
    This is my game project - Top Down City:
    http://www.pascalgamedevelopment.com...y-Topic-Reboot

    My OpenAL audio wrapper with Intelligent Source Manager to use unlimited:
    http://www.pascalgamedevelopment.com...source+manager

  3. #3

    "is" versus "<> nil" on speed?

    My understanding is that [pascal]<> nil[/pascal] is faster due to it being a a simple pointer test, while [pascal]is[/pascal] would incurr more RTTI calls.

    As it has already been said, write a test app to be certain.
    <br /><br />There are a lot of people who are dead while they are still alive. I want to be alive until the day I die.<br />-= Paulo Coelho =-

  4. #4

    "is" versus "<> nil" on speed?

    hmmm... I've been using

    [pascal]if not (obj = nil)[/pascal]

    is this slower than [pascal]if obj <> nil[/pascal] ?

  5. #5

    "is" versus "<> nil" on speed?

    Try compiling the code below and open the CPU window to inspect the generated assembly:

    [pascal]procedure TForm1.Button1Click(Sender: TObject);
    var
    p : tobject;
    begin
    p := nil;
    if p is TForm then
    Tag := 1;
    if Assigned(p) then
    Tag := 3;
    if p<>nil then
    Tag := 4;
    if not (p = nil) then
    Tag := 5;
    end;[/pascal]

    "is" generates a call to a function that tests the rtti-class.
    The other variants generated identical code: a simple test and branch.

    So "is" is slightly slower, the others are identical.
    ZGameEditor - Develop 64kb games for Windows.
    Thrust for Vectrex - ROM-file and 6809 source code.

  6. #6

    "is" versus "<> nil" on speed?

    This is good to know. Cheers.

  7. #7

    "is" versus "<> nil" on speed?

    Quote Originally Posted by savage
    My understanding is that [pascal]<> nil[/pascal] is faster due to it being a a simple pointer test, while [pascal]is[/pascal] would incurr more RTTI calls.
    The difference is subtle, but "is" is implemented by analysing the virtual method tables, not the runtime type information. So, if you want to avoid RTTI bloat, you can still safely use "is".

    "Is" is certainly slower than a nil check.

  8. #8

    "is" versus "<> nil" on speed?

    Quote Originally Posted by jasonf
    hmmm... I've been using

    [pascal]if not (obj = nil)[/pascal]

    is this slower than [pascal]if obj <> nil[/pascal] ?
    This is an easy optimization, most compilers will generate the same code for both, including FPC. However, especially in complex bollean expressions, it can be good forr eadibility to remove some parenthesis.

  9. #9

    "is" versus "<> nil" on speed?

    well, the
    [pascal]if not (obj = nil)[/pascal]
    needs the parenthesis and won't work any other way because the compiler needs the expression being reveresed by the not to be evaluated to a boolean value and the only way that happens is by putting it in parenthesis.

    in the same way as
    [pascal]if a>10 or a<5[/pascal]
    doesn't work

    but

    [pascal]if (a>10) or (a<5)[/pascal]
    does.

    not very easy to read, but required.

  10. #10
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    "is" versus "<> nil" on speed?

    Funny, I often find the parenthesis makes it easy for me to read the code.
    Jason McMillen
    Pascal Game Development
    Co-Founder





Page 1 of 4 123 ... LastLast

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
  •