Return style
If i have the function:
state normal obviously meaning the creature is alive and capable of being collided with. What is the difference between that method and this one:
Is there any minor speed difference, or is it just personal preference/making the code look pretty.
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.
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:
test.o:
(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
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.
It really seems like the sort of case a crappy peep-hole optimizer would spot.
If that is C++, remember to declare your function directly in the header, so that it gets inlined & avoids the function call overhead.
Also, seeing as we're being picky, you want:
null is of type pointer, you're returning boolean so no reason to use null. Use false which is of type boolean.
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.
Just dont have an isAlive function.
Sir, e^iπ + 1 = 0, hence God exists; reply!
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...
And in my acutal game i have return false, i didnt copy paste it and wasnt looking at it either so yea...
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.
No it isn't. It encapsulates functionality and, when declared in the header as I suggested, has zero overhead (it gets inlined).
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.
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.
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.
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.
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).
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).
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
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Handling a return or enter key event on an edit text control | monteboyd | 7 | 4,962 |
Nov 7, 2005 03:29 PM Last Post: monteboyd |
|

