Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Newbie Question: C vs C++

Newbie Question: C vs C++

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++visual-studioperformance
9 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tguzella
    wrote on last edited by
    #1

    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

    T M J Z J 5 Replies Last reply
    0
    • T tguzella

      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

      T Offline
      T Offline
      Tim Smith
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • T tguzella

        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

        M Offline
        M Offline
        markkuk
        wrote on last edited by
        #3

        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++

        1 Reply Last reply
        0
        • T tguzella

          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

          J Offline
          J Offline
          John R Shaw
          wrote on last edited by
          #4

          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

          A 1 Reply Last reply
          0
          • J John R Shaw

            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

            A Offline
            A Offline
            Anonymous
            wrote on last edited by
            #5

            Just wirte your assembly code as inline and make the function declaration as naked this will remove all c++ prolog and epilog to a function (like stack checking, parameter checking etc etc). if you do this then you dont have to worry what compier does.

            1 Reply Last reply
            0
            • T tguzella

              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

              Z Offline
              Z Offline
              ZoogieZork
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • T tguzella

                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

                J Offline
                J Offline
                John M Drescher
                wrote on last edited by
                #7

                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

                T 1 Reply Last reply
                0
                • J John M Drescher

                  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

                  T Offline
                  T Offline
                  tguzella
                  wrote on last edited by
                  #8

                  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...

                  J 1 Reply Last reply
                  0
                  • T tguzella

                    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...

                    J Offline
                    J Offline
                    John M Drescher
                    wrote on last edited by
                    #9

                    .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

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups