Thread: C/C++ vs assembly: speed comparison

  1. #1

    C/C++ vs assembly: speed comparison

    I don't know if this is the right board to ask this on, but anyway...

    I've often read that high level languages such as C and C++ are slower than coding in assembly. My question, then, is why? Doesn't the compiler convert the C/C++ code into machine code anyway, just as what happens with assembly code? If so, both are being converted to machine code, so what's the diff? Or is the conversion just not as efficient with high level languages?

    I think I've read that C is only about 1-2% slower than assembly, but I'm quite sure what's the story with C++. I did once read a paper saying that it wasn't as fast as C, but I can't remember any exact numbers. Anyone had any first-hand experience in comparing their speed?



  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Depending on how you code obviously an exe produced from c++ will probably run slightly slower than the same exe written in c. This is so marginal however that it should be practically unnoticeable.
    The same exe made in hand coded assembler is likely to be up to five times faster than the same exe produced by a c++ compiler. Compilers just make there machine code at the end of the day from look up tables. There will always be ways of doing things differently and faster in hand coded assembly than the code your compiler is capable of producing.
    The real difference comes in portability and coding time saved.
    An average sized project in mfc may take 3-6 months.
    The same in c+win32 api maybe 18 months.
    hand coded assembler probably 3 years.

    Correctly written the c and c++ versions of a program can be source-code portable. The assembly language program will only ever run on the processor architechture it was written for.

    To check out some simple speed differences that are capable between c++ and asm and other languages look at the tutorials here .NeHe has tuts written in many different languages all doing the same things. Try comparing the c++ exe with the hand coded asm exes.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :-

  3. #3
    I think it's a misconception that C is faster than C++, after all C++ is just a better C. I'm pretty sure this myth is perpetrated by those who don't want to move over to C++. Most C/C++ compilers have really good optimization and writing something in assembly is not gonna produce the desired gains over coding in a high level language today. Go here, and check it out. They say that most large games today don't have any Assembly language at all in them - that code written C++ can be optimized by the compiler and produce faster execution.

    You should learn how assembly works, you may need it some time and it's good at teaching you how the computer works, but nowadays the compiler can optimize nearly as efficiently as pure asm code so it's not really worth writing any assembly at all.
    Last edited by OneStiffRod; 11-23-2002 at 06:05 PM.
    My Avatar says: "Stay in School"

    Rocco is the Boy!

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    There is a small speed penalty with C++ (RTTI, Polymorphism, Multiple Inheritance).....often it doesnt matter.......For games, I guess it does....From most of my reading, ASM is still used today in games where its benefits are needed.....but then I dont make games........Sayeh and others know a lot more than me on games and how ASM fits in there, so for a better (and probably more merciless answer ) I'll let them respond!

  5. #5
    Thanks for the replies, but I think you underestimate how much slower C++ is compared to C. I don't think it's a myth at all put out by C++ haters. I mentioned I once saw a paper on this, and I managed to find it again: It's called "An empirical comparison of C, C++, Java, Perl, Python, Rexx, and Tcl" by Lutz Prechelt (IEEE Computer). If I remember correctly, the title has changed slightly - the copy I have is one before it was published.

    Anyway, I don't have time to reread the entire thing now, but I'm looking at one of his graphs showing running times for all of the languages tested for a particular problem, with several implementations for each language. Reading off the graph, the median running time for the C implementations for this particular problem was less than 4 seconds, while C++ appears to be just under 64 seconds. Heck, C++ is even worse than Java! That can't be right...

    Hmmm... just read a bit of the text, and it says that C++ is in fact faster than Java after all - medians are misleading. But the fact remains: C is far quicker than C++. Then again, only a handful of implementations for C were programmed, so again this is probably isn't the best comparison possible, but it's all I got.

    Something else interesting from the article: they measure the total time required to program the implementations. Again I'm just looking at the graph and not reading the text, but it looks like, out of C, C++, and Java, C required the least number of hours to code the implementation! Second was Java, and requiring the most amount of time was C++. Hmmm... this is also disturbing (no, I didn't start this thread to diss C++. I really didn't. Aaah well...)


  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Somne of the things I mentioned earlier will naturally give a little overhead on C++ when compare to C......but the actual efficiency penalty will depend on lots of things not least the task the code has been created for and the tools employed (modern compilers are far better at optimising that earlier tools)

    As to the C++ being faster than Java......hmm....have you ever tried Java and seen how fast is is? If so you would like me take that test you refer to with a grain of salt......C++ (like C) compiles to naitive code, Java doesnt.....

    The rule of thumb that C++ designers worked to was within (i think) 5% less efficient than C........and in most cases the penalty is unnoticable...If those testers completed a task in C in 4 seconds, and then found it took 64 seconds in C++, then I would imagine their tools were junk and/or they coded it very badly indeed

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    >Thanks for the replies, but I think you underestimate how much slower C++ is compared to C.
    Which type of speed do you mean? Actual run-time performance is and always has been comparable to C, this was one of the primary design goals of C++. Compilation time is very different, C is quick and C++ is still very slow, though the actual differences vary between compilers.

    My best code is written with the delete key.

  8. #8
    "Which type of speed do you mean? "

    I meant running time, not compilation time. Like everyone else I question the results of the paper... but more disturbing is CODING time, where C++ was worst out of C, Java and C++.


  9. #9
    Visionary Philosopher Sayeh's Avatar
    Join Date
    Aug 2002
    I think it's a misconception that C is faster than C++, after all C++ is just a better C.
    C++ is slightly slower in execution than C-- that is because of the existing compiler technology, not a fault of the language. C++ is not a "better C", either-- it is merely a set of extensions to the C language. You cannot know C++ and not know C.

    In some cases, the slight performance drop is not noticable at all. In others, noteably leading edge games, it can be very noticable.

    As for performance of assembler versus any other language, of course direct assembler will be faster-- why? Here's why--

    1) No stackframes to maintain (stacks and stack frames are different)
    2) No massive assortment of variables-- you work with registers.
    3) code is specific, not general purpose

    Compilers do not "figure out" what you are trying to do and "hand-code" the assembler to fit your need. They look at what you've written and match it to an example in their code library. If the match is close, they then pull a general purpose snippet of assembler out and stuff it into the disassembly file they are creating. and move on to the next code.

    And of course, a great deal of it has to do with how good the programmer themselves is.

    As an Example: Last year, a group of us here wrote a code frag that would simply invert the bits in a byte (flip them end for end). The test was perfomed 1000 times. The C++ example with if statements, took 47 seconds. The C example using approximately 8 lines and shifts took 12 seconds, and the C example using a table lookup took 7 seconds. Assembler would probably have come in between 5 and 6 seconds.

    Now, that is not to say that C++ would always generate such a disparate gap-- no doubt the C++ example at the time was poorly written.
    It is not the spoon that bends, it is you who bends around the spoon.

  10. #10
    Why would you flip bits in C++ with if statements and not the ~operator?? It seems like C was given an unfair advantage in that test, probably in all the "quoted" tests.

    Here's a link to an article - it is old but points to some things I was talking about.

    No one here doubts the speed of assembler but today's compilers - especially the Enterprise versions that MS tries to get you to pay $5,000+ for can optimize code closest to pure assembler speeds.

    Every article I could find that dissed C++ was old and reffered to the current generation of compilers being unable to create code comprable to C but today is different and Procedural programming in C++ should create the same speed execution as C. There is more overhead using classes instead of structs but it's trivial in today's PC's. There are important improvements that C++ has over C such as operator and function overloading, inheritance, better scope and type checking. There are many things that you can do in C++ just with function overloading that would require more coding in C and thusly produce slower execution.

    C++ speed compared to C, the difference is almost NILL.

    **I'm still looking for that article on that stated that code can be optimized without the need for assembler- maybe one of ya'll can find it before me and post the link - I just read it last Friday so it's current.**

    Let me add this:
    Last edited by OneStiffRod; 11-25-2002 at 03:05 PM.
    My Avatar says: "Stay in School"

    Rocco is the Boy!

  11. #11
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Ontario, Canada
    >Why would you flip bits in C++ with if statements and not the ~operator?? It seems like C was given an unfair advantage in that test, probably in all the "quoted" tests.

    I think he means that if you have 10111011 in, it would be reversed, giving 11011101 out.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  12. #12
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    that's exactly right StiffRod. People who want to prove C++ slow usually do things like that. In my company they decided that C++ was slower by doing an algorithm loaded with dynamic allocation of classes in C++. The same algorithm in C using a pre-allocated array of structures was of course faster. It's a false conclusion based on ignorant tests.

    edit: XSquared cleared up that ~ issue but your point is still valid.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. Speed comparison
    By deviousdexter in forum C# Programming
    Replies: 2
    Last Post: 11-10-2008, 04:42 PM
  3. Speed comparison between vector and 2*2 array
    By cunnus88 in forum C++ Programming
    Replies: 5
    Last Post: 11-25-2007, 02:05 AM
  4. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  5. C,C++,Perl,Java
    By brusli in forum C Programming
    Replies: 9
    Last Post: 12-31-2001, 03:35 AM