Return style

Member
Posts: 102
Joined: 2005.01
Post: #1
If i have the function:

Code:
public boolean isAlive()    {
    return (state == STATE_NORMAL);
}

state normal obviously meaning the creature is alive and capable of being collided with. What is the difference between that method and this one:

Code:
public boolean isAlive()    {
    if (state == STATE_NORMAL)    {
        return true;
    }
    return null;
}


Is there any minor speed difference, or is it just personal preference/making the code look pretty.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
The former is more efficient:

(This done with GCC 3.4.2 on Windows -- ugh -- but GCC 4.0 on Mac/Intel is likely to be the same)

test.c:
Code:
int state;

int isAlive() { return (state == 0); }
int isAlive2() { if (state == 0) return 1; return 0; }

test.o:
Code:
Disassembly of section .text:

00000000 <_isAlive>:
   0:    55                       push   %ebp
   1:    89 e5                    mov    %esp,%ebp
   3:    83 3d 00 00 00 00 00     cmpl   $0x0,0x0
   a:    0f 94 c0                 sete   %al
   d:    0f b6 c0                 movzbl %al,%eax
  10:    5d                       pop    %ebp
  11:    c3                       ret    

00000012 <_isAlive2>:
  12:    55                       push   %ebp
  13:    89 e5                    mov    %esp,%ebp
  15:    83 ec 04                 sub    $0x4,%esp
  18:    83 3d 00 00 00 00 00     cmpl   $0x0,0x0
  1f:    75 09                    jne    2a <_isAlive2+0x18>
  21:    c7 45 fc 01 00 00 00     movl   $0x1,0xfffffffc(%ebp)
  28:    eb 07                    jmp    31 <_isAlive2+0x1f>
  2a:    c7 45 fc 00 00 00 00     movl   $0x0,0xfffffffc(%ebp)
  31:    8b 45 fc                 mov    0xfffffffc(%ebp),%eax
  34:    c9                       leave  
  35:    c3                       ret
Quote this message in a reply
sh4ggy87
Unregistered
 
Post: #3
OSC's test code generates the same assembly on gcc 3.3.4 at -O2 and -O3... (both x86 and ppc)

It really seems like the sort of case a crappy peep-hole optimizer would spot.
Quote this message in a reply
Moderator
Posts: 771
Joined: 2003.04
Post: #4
If that is C++, remember to declare your function directly in the header, so that it gets inlined & avoids the function call overhead.
Quote this message in a reply
Member
Posts: 72
Joined: 2004.06
Post: #5
Also, seeing as we're being picky, you want:

Code:
public boolean isAlive()    {
    if (state == STATE_NORMAL)    {
        return true;
    }
    return false; //false here, not null
}

null is of type pointer, you're returning boolean so no reason to use null. Use false which is of type boolean.

"So long and thanks for all the fish" - In memory of Douglas Adams.
My Site
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #6
Just dont have an isAlive function.

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Member
Posts: 102
Joined: 2005.01
Post: #7
Why would i not want an isAlive function?

And in my acutal game i have return false, i didnt copy paste it and wasnt looking at it either so yea...
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #8
If it's simply an equality check, than there's no real point in having it. It's simply extra overhead of creating then destroying the stack frame.
Quote this message in a reply
Moderator
Posts: 771
Joined: 2003.04
Post: #9
No it isn't. It encapsulates functionality and, when declared in the header as I suggested, has zero overhead (it gets inlined).
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #10
Wouldn't it only get inlined if it's defined in the header? It doesn't know where the function actually is (or what it does) until linking, so it can't possibly inline it unless you define it in the header.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #11
uh, that's what the man said...

Yes, having a function is probably good style here, since STATE_NORMAL ain't exactly descriptive of aliveness.

Remember folks, -O2 and below won't inline things you don't ask 'em to.
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #12
I was going by the definition by declaring something meaning you state that it exists, while defining something is actually implementing it. If PowerMacX really meant the latter, then I will concede.
Quote this message in a reply
Moderator
Posts: 771
Joined: 2003.04
Post: #13
I thought it was clear, but yes I meant the later. I used "declare" in reference to putting the definition directly in the declaration.
That's not the only option, tough. It is possible to define a method after the class declaration and get it inlined, but it still needs to be in the header (and adding an inline keyword may be necessary, although the compiler may choose to ignore it).
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #14
the only rule about inlining is that the compiler must have seen the function's body before it sees the call site, and even that may not be a hard and fast rule in recent versions of GCC (not sure that the 4.0 Apple's using is recent enough though). Header files are technically irrelevant, though certainly convenient Smile
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Handling a return or enter key event on an edit text control monteboyd 7 5,789 Nov 7, 2005 03:29 PM
Last Post: monteboyd