Newbie Question: C vs C++
-
Hi there I have a question: how does a c++ compiler generate code?? does it convert the c++ code to c and then to assembly?? I am asking that because i need to write a number crunching program (it needs to be as fastest as possible), and i am thinking of initially writing it in C++ (i love C++'s OOP), then going to assembly. I am willing to sacrifice ease of programming in favor of speed, so should i write it in C or stay with C++?? Thanks Thiago Guzella
-
Hi there I have a question: how does a c++ compiler generate code?? does it convert the c++ code to c and then to assembly?? I am asking that because i need to write a number crunching program (it needs to be as fastest as possible), and i am thinking of initially writing it in C++ (i love C++'s OOP), then going to assembly. I am willing to sacrifice ease of programming in favor of speed, so should i write it in C or stay with C++?? Thanks Thiago Guzella
Most compilers go from the language to a generic pcode format. Then the linker takes the pcode and generates machine code while doing some high level optimizations. In general, C++ is just as fast as C but it can be easy to do silly stuff that slows C++ down. Also, with the current crop of optimizers, converting the C++/C code to asm usually is more of a waste than a benefit. Tim Smith I'm going to patent thought. I have yet to see any prior art.
-
Hi there I have a question: how does a c++ compiler generate code?? does it convert the c++ code to c and then to assembly?? I am asking that because i need to write a number crunching program (it needs to be as fastest as possible), and i am thinking of initially writing it in C++ (i love C++'s OOP), then going to assembly. I am willing to sacrifice ease of programming in favor of speed, so should i write it in C or stay with C++?? Thanks Thiago Guzella
The first C++ compiler (AT&T Cfront) used C as an intermediate step, but nearly all current C++ compilers create machine code directly. The only exception I know is Comeau C++
-
Hi there I have a question: how does a c++ compiler generate code?? does it convert the c++ code to c and then to assembly?? I am asking that because i need to write a number crunching program (it needs to be as fastest as possible), and i am thinking of initially writing it in C++ (i love C++'s OOP), then going to assembly. I am willing to sacrifice ease of programming in favor of speed, so should i write it in C or stay with C++?? Thanks Thiago Guzella
I see the code generation question have been aswered. Now for speed. C is closer to asm than C++ and is therefore the best place to start if you think you may want to convert the code to asm. The conversion is not normaly needed as modern compilers are very good at optimizing. There is the possiblity that you could improve on the asm code produced by the compiler, but fist let it generate the code for you to examine what it has done. Also, make sure that it is being compiled as C code (not C++) or it might contain unneeded overhead. Good luck! INTP
-
I see the code generation question have been aswered. Now for speed. C is closer to asm than C++ and is therefore the best place to start if you think you may want to convert the code to asm. The conversion is not normaly needed as modern compilers are very good at optimizing. There is the possiblity that you could improve on the asm code produced by the compiler, but fist let it generate the code for you to examine what it has done. Also, make sure that it is being compiled as C code (not C++) or it might contain unneeded overhead. Good luck! INTP
-
Hi there I have a question: how does a c++ compiler generate code?? does it convert the c++ code to c and then to assembly?? I am asking that because i need to write a number crunching program (it needs to be as fastest as possible), and i am thinking of initially writing it in C++ (i love C++'s OOP), then going to assembly. I am willing to sacrifice ease of programming in favor of speed, so should i write it in C or stay with C++?? Thanks Thiago Guzella
If there's one thing I learned when I started diving into building my own compiler, it's that modern C and C++ compilers are smart. Very smart. That said, they aren't perfect, and sometimes they'll focus optimizations on parts of your code that aren't as critical, or they'll refuse to do certain optimizations because it cannot be proven from the language definition that the optimization is safe. And, of course, they can't magically pick a better algorithm for you. What this means in practical terms is that you shouldn't try to do premature optimizations. Write it in the language of your choice. Benchmark it. Profile it. You might just find that it's fast enough already. If not, you can find the bottlenecks and then break out the assembly. - Mike, who has wasted entire too much time optimizing for code which wasn't a problem.
-
Hi there I have a question: how does a c++ compiler generate code?? does it convert the c++ code to c and then to assembly?? I am asking that because i need to write a number crunching program (it needs to be as fastest as possible), and i am thinking of initially writing it in C++ (i love C++'s OOP), then going to assembly. I am willing to sacrifice ease of programming in favor of speed, so should i write it in C or stay with C++?? Thanks Thiago Guzella
First off do not use .NET managed C++. I would program it in C++ and add assembly where needed. One case in point is if you have to do any float to int calculations do not let the compiler do that. The cpu does rounding by default and the standard states it should be truncated. So the compiler inserts over 100 asm instructions to reverse the rounding process. In a bilinear interpolation algorithm I got a factor of 10 speed up by using asm to do the conversion. Also where possible I would use MMX, and SIMD. John
-
First off do not use .NET managed C++. I would program it in C++ and add assembly where needed. One case in point is if you have to do any float to int calculations do not let the compiler do that. The cpu does rounding by default and the standard states it should be truncated. So the compiler inserts over 100 asm instructions to reverse the rounding process. In a bilinear interpolation algorithm I got a factor of 10 speed up by using asm to do the conversion. Also where possible I would use MMX, and SIMD. John
thanks... that's what i am kinda thinking about: writing the program in C++ under visual c++ 6, and using inline assembly to optimize where necessary for mmx/3dnow/sse. i could do that under .net using intrinsics, but i don't feel much confortable with that...
-
thanks... that's what i am kinda thinking about: writing the program in C++ under visual c++ 6, and using inline assembly to optimize where necessary for mmx/3dnow/sse. i could do that under .net using intrinsics, but i don't feel much confortable with that...
.NET is ok as long as you do not use the .NET CLR runtime. I do have one warning though. To use the mmx/3dnow/sse instructions in inline assembly on VC5 or VC6 you need the processor pack. The problem is the processor pack breaks exceptions (try() catch() blocks) in some cases and this could cause your program to crash. This is why I would recommend the VC.NET. It has builtin support for mmx/3dnow/sse. John