An Introduction to Virtual Memory Virtual memory is why you can be so wasteful with your buffers! Interviews and Resumes Interview insights from the perspective on an interviewer. Writing Multithreaded Applications in C++ (the right way) Concurrency is already difficult. Read about C++'s modern facilities to help you! Developing an Online Community Because people are what makes the internet useful! A Refresher on Pointers Because they're important and they shouldn't be difficult to understand! C is not (necessarily) Faster than C++
I’ve recently read a few things online (i.e. stackoverflow questions, random forum posts, and observations on IRC) asking whether or not C is faster than C++. What is more alarming to me is that there is a surprisingly large number of people who say, “yes! C is faster than C++ since it’s lower to the hardware.” Sorry to say (read: sorry I’m not sorry…), but this is not exactly correct.
We’ll first describe why this assumption doesn’t make sense and then we’ll go through a few examples. To begin, both C and C++ are compiled languages which compile directly to machine code. What this means is that after you compile a C program and a C++ program, their binaries are in the same machine language. They may look slightly different (i.e. C doesn’t have use for vptr’s), but above all, the binary will be a sequence of add’s, mul’s, div’s, sub’s, and branches. More importantly is that– in theory– looking at a piece of compiled C or C++ code could produce exactly the same resulting binary. I do admit that this scenario is highly unlikely as it is all very compiler dependent and proper C++ should be written differently than proper C, but it is theoretically possible. This is an important point because it immediately shows us that C++ does not actually have any inherent flaws which naturally make it slower than C. That being said, the differences here in the final binaries will come from the respective compilers and not from the languages themselves.
At this point you may be considering other compiled languages such as Java and everyone knows Java is less performant than C. My first rebuttal is that performant C is faster than Java. Poorly written C code can also be slow and can certainly be slower than Java if the Java programmer knew what he or she was doing. More technically, however, Java runs in the Java Virtual Machine (JVM). That is, it runs inside of another program. Moreover, it uses garbage collection for memory management. Those these language decisions are useful in many ways (i.e. easy cross-platform code, no worrying about memory management), they incur a certain amount of overhead. As a result, a carefully written C or C++ program can certainly be faster than Java. But more importantly, this reiterates what I mentioned above. Neither C nor C++ have these overhead constraints and are fully capable of producing low (no?)-overhead binaries. As you ponder more languages (i.e. python) consider in what environments they run in. If they are not running natively, they are incurring overhead somewhere.
Now that we’ve discussed why C and C++ should be equally as powerful (barring the effectiveness of their respective compilers), let us go through a few examples. For all of my examples I will use gcc version 4.8.2 where gcc will be used for compiling C code and g++ for C++ code. I will keep optimizations turned off (i.e. -O0 switch in gcc) since we are trying to look at the languages and not the ability of the compilers. Moreover, I will also use a similar command to get the average results:
An Argument for Producing Open Source Content Think zen... Understanding Value Value add, value add, value add. It's all about that value add! Facilitating and Participating in Intelligent Discourse Because not all conversation is productive. Language Agnosticism Leads to Real Software Engineering You shouldn't be a programmer of language "X"; you should just be a programmer.