Click here to Skip to main content
15,884,838 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Java vs. C++ Pin
jhwurmbach18-Sep-07 4:53
jhwurmbach18-Sep-07 4:53 
GeneralRe: Java vs. C++ Pin
Russell'18-Sep-07 5:11
Russell'18-Sep-07 5:11 
GeneralRe: Java vs. C++ Pin
jhwurmbach18-Sep-07 5:25
jhwurmbach18-Sep-07 5:25 
GeneralRe: Java vs. C++ Pin
Russell'18-Sep-07 5:44
Russell'18-Sep-07 5:44 
GeneralRe: Java vs. C++ Pin
El Corazon18-Sep-07 8:22
El Corazon18-Sep-07 8:22 
GeneralRe: Java vs. C++ Pin
jhwurmbach18-Sep-07 8:31
jhwurmbach18-Sep-07 8:31 
GeneralRe: Java vs. C++ Pin
El Corazon18-Sep-07 9:01
El Corazon18-Sep-07 9:01 
AnswerRe: Java vs. C++ Pin
Mike Dimmick18-Sep-07 4:19
Mike Dimmick18-Sep-07 4:19 
It very much depends on the actual problem, and on the optimization settings you select, for C++. I would expect that a decent optimizer could detect that the operations within the loop cancel each other out and it might even conclude that the loop itself doesn't do anything, leaving you with no code whatever. Indeed, when run with the optimization options /Oxs, that is exactly what Visual Studio 2005 does. So in fact does Visual C++ 6.0.

I was trying to make it do more work, so asked it to print the final values of j and k. It simply passed the literals 5 and 5.0 into printf. I used the /FAs switch to generate an assembly listing with source code. This can be daunting if you're not familiar with x86 assembly, but it's pretty obvious that the compiler didn't generate any instructions for the loop. (It even did crazy tricks like loading the address of GetTickCount, which I used for time measurement, into a register!)

On my computer a debug build, with optimizations disabled, took 23 seconds to execute. The 'optimizations disabled' version is incredibly naive, as close to a literal translation as possible, and actually performs the operations as coded, although it performs some straightforward conversions (e.g. multiplying and dividing integers by 2 by bit-shifting, and using floating-point addition to multiply k by 2). It also stores results back into main memory rather than just leaving them in a register, for example.

Almost all Java implementations now use a JIT (Just-In-Time) compiler. This is able to perform some of the optimizations that a C++ compiler can do and so can eliminate some of the unnecessary operations (like avoiding write-back to memory for local variables), but not all.

If you want to do a real test, you'll have to actually make the operations somehow dependent on the value of the loop variable, so the compilers don't just throw the code away, and allow the C++ compiler to optimize the code.

Stability. What an interesting concept. -- Chris Maunder

AnswerRe: Java vs. C++ Pin
Nemanja Trifunovic18-Sep-07 4:46
Nemanja Trifunovic18-Sep-07 4:46 
AnswerRe: Java vs. C++ Pin
El Corazon18-Sep-07 5:00
El Corazon18-Sep-07 5:00 
AnswerRe: Java vs. C++ Pin
bob1697218-Sep-07 6:39
bob1697218-Sep-07 6:39 
GeneralRe: Java vs. C++ Pin
jhwurmbach18-Sep-07 8:33
jhwurmbach18-Sep-07 8:33 
QuestionComplier C2664 Error Pin
Andy20218-Sep-07 3:02
Andy20218-Sep-07 3:02 
QuestionRe: Complier C2664 Error Pin
David Crow18-Sep-07 3:06
David Crow18-Sep-07 3:06 
AnswerRe: Complier C2664 Error Pin
Andy20218-Sep-07 4:11
Andy20218-Sep-07 4:11 
GeneralRe: Complier C2664 Error Pin
David Crow18-Sep-07 4:19
David Crow18-Sep-07 4:19 
GeneralRe: Complier C2664 Error Pin
Andy20218-Sep-07 4:29
Andy20218-Sep-07 4:29 
GeneralRe: Complier C2664 Error Pin
David Crow18-Sep-07 4:34
David Crow18-Sep-07 4:34 
GeneralRe: Complier C2664 Error Pin
Andy20218-Sep-07 4:59
Andy20218-Sep-07 4:59 
QuestionRe: Complier C2664 Error Pin
David Crow18-Sep-07 5:14
David Crow18-Sep-07 5:14 
AnswerRe: Complier C2664 Error Pin
Andy20218-Sep-07 6:10
Andy20218-Sep-07 6:10 
QuestionPrinting Problem Pin
pri_skit18-Sep-07 1:40
pri_skit18-Sep-07 1:40 
QuestionUse MFC in.... Pin
himuskanhere18-Sep-07 1:36
himuskanhere18-Sep-07 1:36 
AnswerRe: Use MFC in.... Pin
nbugalia18-Sep-07 1:41
nbugalia18-Sep-07 1:41 
AnswerRe: Use MFC in.... Pin
Iain Clarke, Warrior Programmer18-Sep-07 1:46
Iain Clarke, Warrior Programmer18-Sep-07 1:46 

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.