Fashionable, but unable to tell fact from fiction (testing4l) wrote,
Fashionable, but unable to tell fact from fiction

So cmoog made the mistake of mentioning how great C++ is.

Being a special interest of mine, I decided to see the current state of things between C and C++ at the moment.

So, two simple programs.

#include <stdio.h>

int main(void)
   puts("Hello World!");
   return 0;


#include <iostream.h>

int main(void){
cout<<"Hello World!\n";
return 0;

Both compiled with gcc -O3
$ ls -l chello cpphello
-rwxr-xr-x    1 root     root         6691 Jan 18 17:28 chello
-rwxr-xr-x    1 root     root         8136 Jan 18 17:27 cpphello

$ time ./chello
Hello World!

real    0m0.004s
user    0m0.001s
sys     0m0.002s

$ time ./cpphello
Hello World!

real    0m0.009s
user    0m0.006s
sys     0m0.002s

Those are representative times (I ran each about 10 times or so and averaged in actuality)

System times are about the same. That makes sense since that's linux printing it to the screen.

Real times are mostly junk. That's the time from start to stop of execution, regardless of what else is going on
in the system.

User times are where it's at. That's the actual time that the code is paged in and executing.

One might notice that C++ takes longer. Significantly longer. This is because of the runtime allocation of the objects in question and cleanup probably. C doesn't bother with that. It interacts directly. This is why C will always beat out C++.

Object Oriented Programming imposes all sorts of strange limitations upon a programmer that aren't true to the machine's capabilities. It's all under the vague and poorly defended pretense that it creates better code. The nastiest bugs that I've ever tracked down have been the result of obfuscation through OOP. In order to do almost any operation, you invoke all sorts of classes one after the next. Many of which are internal compiled libraries which might have interesting issues with cleanup and memory management.

C code when properly placed between files shows the same benefits as distinct objects. C code when properly placed in functions results in clean, readable, compartmentalized code. More of it's under your command and thanks to call stacks, you can easily track down just which function caused the problem.

You also don't have those silly private, public, and protected distinctions.
  • Post a new comment


    default userpic

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 1 comment