Click here to Skip to main content
15,921,531 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralMSVC 2003 GUI Help Pin
c++code10112-Jan-05 14:31
c++code10112-Jan-05 14:31 
GeneralSending output to a printer Pin
BRIMID12-Jan-05 13:29
BRIMID12-Jan-05 13:29 
GeneralMFC socket programing problem Pin
shaihnc12-Jan-05 12:39
shaihnc12-Jan-05 12:39 
GeneralRe: MFC socket programing problem Pin
shaihnc13-Jan-05 5:32
shaihnc13-Jan-05 5:32 
GeneralSpeed of operators Pin
Malcolm Smart12-Jan-05 11:57
Malcolm Smart12-Jan-05 11:57 
GeneralRe: Speed of operators Pin
Rick York12-Jan-05 12:18
mveRick York12-Jan-05 12:18 
GeneralRe: Speed of operators Pin
Mike Dimmick12-Jan-05 12:19
Mike Dimmick12-Jan-05 12:19 
GeneralRe: Speed of operators Pin
Mike Dimmick12-Jan-05 12:59
Mike Dimmick12-Jan-05 12:59 
OK, I loaded up VS2005 Beta 1 and pasted your code into a new project. In the same file I added main:
int a = A_Return_n_if_n_isIsLessThan2( argc );
int b = B_Return_n_if_n_isIsLessThan2( argc );
int c = C_Return_n_if_n_isIsLessThan2( argc );
int d = D_Return_n_if_n_isIsLessThan2( argc );
 
printf( "%d\n%d\n%d\n%d\n", a, b, c, d );
 
return 0;
I then compiled with the /FAs option to generate assembly listings with source, using the Full Optimization, Favor Small Code options (/Oxs). A and C got compiled to the same code:
    mov  eax, DWORD PTR _n$[esp-4]  ; eax = n
    cmp  eax, 2                     ; set flags based on eax - 2
    jb   SHORT $LN4@C_Return_n      ; jump if below
    or   eax, -1                    ; set eax to -1
$LN4@C_Return_n:
    ret  0                          ; return, remove 0 items from the stack
In reading this it's important to know that the eax register contains return value of the function. If the value is below 2 the jump is taken and eax already contains the return value. The compiler picked or rather than mov because it can use sign-extension to fill the whole register and therefore only needs to specify 0xFF rather than 0xFFFFFFFF for the mov instruction, saving 2 bytes (3 vs. 5).

B and D compile to this:
    mov	eax, DWORD PTR _n$[esp-4]
    test eax, eax               ; test sets the Zero Flag if zero
    je   SHORT $LN3@D_Return_n  ; je (jump equal) jumps if ZF is set
    cmp  eax, 1                 ; set flags based on eax - 1
    je   SHORT $LN3@D_Return_n  ; if zero, jump
    or   eax, -1
$LN3@D_Return_n:
    ret  0
Interestingly, the compiler actually removed the temporaries a, b, c and d from main and just compiled the calls inline, in the opposite order. Presumably the optimizer looked inside the calls, saw there were no side-effects, eliminated the sequence points that should have been at the end of each statement, and reordered the calls so it could push the results on the stack in the order needed for printf.

Therefore B/D might be slightly slower than A/C. However, on modern processors you can't count instructions and say the one with more instructions will be slower. You have to take parallel execution, out-of-order execution, and branch prediction into account. If enough resources are available the processor could evaluate the test, cmp and or in B/D in parallel, discarding the 'wrong' result. However A/C is smaller code and is more likely to fit on a memory page with other code, will fit better into instruction cache and uses fewer branch prediction resources. You're unlikely to see any difference, though.

There's very little point performing micro-optimisations like this unless you already know that there's a bottleneck here, which is pretty unlikely. Write whatever's clearest.

Stability. What an interesting concept. -- Chris Maunder
GeneralRe: Speed of operators Pin
Ryan Binns12-Jan-05 16:58
Ryan Binns12-Jan-05 16:58 
GeneralOnSize Function : Positioning Static Controls Pin
tnguyen44412-Jan-05 11:50
tnguyen44412-Jan-05 11:50 
GeneralRe: OnSize Function : Positioning Static Controls Pin
User 1278212-Jan-05 16:13
User 1278212-Jan-05 16:13 
GeneralRe: OnSize Function : Positioning Static Controls Pin
Ryan Binns12-Jan-05 17:01
Ryan Binns12-Jan-05 17:01 
GeneralRe: OnSize Function : Positioning Static Controls Pin
tnguyen44413-Jan-05 6:10
tnguyen44413-Jan-05 6:10 
GeneralForms Problem (new to Win32 programming) Pin
LighthouseJ12-Jan-05 11:30
LighthouseJ12-Jan-05 11:30 
GeneralChecking Namedpipe Access Pin
humps12-Jan-05 10:43
humps12-Jan-05 10:43 
GeneralSetActivePage in property page Pin
DanYELL12-Jan-05 9:58
DanYELL12-Jan-05 9:58 
GeneralRe: SetActivePage in property page Pin
Michael Dunn12-Jan-05 10:23
sitebuilderMichael Dunn12-Jan-05 10:23 
GeneralRe: SetActivePage in property page Pin
DanYELL12-Jan-05 12:28
DanYELL12-Jan-05 12:28 
GeneralRe: SetActivePage in property page Pin
Michael Dunn12-Jan-05 14:50
sitebuilderMichael Dunn12-Jan-05 14:50 
Generalbrowsing DLL resource files through CFileDialog Pin
(Steven Hicks)n+112-Jan-05 8:13
(Steven Hicks)n+112-Jan-05 8:13 
GeneralRe: browsing DLL resource files through CFileDialog Pin
Neville Franks12-Jan-05 8:57
Neville Franks12-Jan-05 8:57 
GeneralRe: browsing DLL resource files through CFileDialog Pin
Ryan Binns12-Jan-05 17:06
Ryan Binns12-Jan-05 17:06 
GeneralWinsock recv help Pin
KnaveR77712-Jan-05 7:19
KnaveR77712-Jan-05 7:19 
GeneralRe: Winsock recv help Pin
greba12-Jan-05 7:58
greba12-Jan-05 7:58 
GeneralRe: Winsock recv help Pin
KnaveR77712-Jan-05 9:15
KnaveR77712-Jan-05 9:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.