Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Huge speed difference between identical c and c++ programs

Tags:

c++

performance

c

I'm very new to c and c++ programming, so I'm starting with the basics. I wrote identical Fibonacci loop programs for both c and c++ to test relative speed. I thought they'd be about the same for something so simple, but the c++ version is 60x slower. All they do is loop through and print the first 14 Fibonacci numbers 10,000 times. Here is the c version:

#include <stdio.h>

int main (){
    int c = 0;
    int x, y, z;

    while(c < 10000)
    {
        x = 0;
        y = 1;
        while(x < 255)
        {
            printf("%d\n", x);
            z = x + y;
            x = y;
            y = z;
        }
        c++;
    }
    return 0;
}

and here is the c++ version:

#include <iostream>
using namespace std;

int main()
{
    int c = 0, x = 0, y = 0, z = 0;
    while(c < 10000)
    {
        x = 0;
        y = 1;
        while(x < 255)
        {
            cout << x << endl;
            z = x + y;
            x = y;
            y = z;
        }
        c++;
    }
    return 0;
}

I wrote both in notepad++ and compiled them using g++ from the mingw that comes with codeblocks:

g++ -o fibc.exe fib.c -s
g++ -o fibcpp.exe fib.cpp -s

The executables are very different in size: the c is 8.5KB and the c++ is 784KB! I used powershell to time them:

Measure-Command {start-process "C:\Path\fibcpp.exe" -RedirectStandardOutput "C:\Path\cpp.txt" -Wait}

The files produced are identical, but the c version took 1 sec and the c++ verison took 60sec! (In fact, putting a loop of 1 million for the c program still only took 13sec). I also wrote the c++ in Visual Studio 17 and compiled it there with an x86 release config. The program size is now 9.5KB, but the run time is the same as the g++ version: 62sec. Why is this happening for such a simple program?

like image 359
MrPuzzler Avatar asked Aug 23 '19 11:08

MrPuzzler


People also ask

Are C programs faster than C++?

Performance is slow compared to C++. C++ language is an object-oriented programming language, and it supports some important features like Polymorphism, Abstract Data Types, Encapsulation, etc. Since it supports object-orientation, speed is faster compared to the C language.

Is pure C faster than C++?

The same code in C and C++ should usually run at exactly the same speed, the exception being code that has different semantics due to different aliasing rules, etc.

Which is faster C or C#?

Performance: C++ is widely used when higher level languages are not efficient. C++ code is much faster than C# code, which makes it a better solution for applications where performance is important.

How much faster is C++ compared to C#?

Total Run times: C#: 289ms, C++ 152ms (roughly 90% faster)


2 Answers

You are benchmarking printf vs cout, since these are the major bottlenecks in your program.

printf is a terribly slow function, but probably still faster than osteam, which will have to maintain its own private hell of template meta programming, to keep it as flexible as required by the C++ standard. cout is definitely far more flexible and complex than printf. More features means slower code.

If you truly want to compare the two languages, drop the print functions and replace them with a dummy function "foo", with external linkage:

void foo (int x);
...

while(x < 255)
{
  foo(x);

When benchmarking that code with gcc -O3 for x86, I get almost identical code for C version and C++ version.

The only notable difference is the "CRT" goo that C++ added at the end of main(). It is calling atexit and various other clean-up that C will do too, but perhaps outside the application code. Still there will always be overhead in C++, because it has to call constructs and clean-up destructors of objects with static storage duration. C doesn't have to do this.

like image 79
Lundin Avatar answered Oct 13 '22 23:10

Lundin


The reason why C++ cout is slow is explained in here.

By default, iostream objects and cstdio streams are synchronized (as if this function was called with true as argument).

So by default cout is synced with stdio.

Try executing the following to speed up.

ios_base::sync_with_stdio(false)

But be careful, this code will print something you don't expect.

#include <iostream>
#include <cstdio>

int main()
{
    std::ios::sync_with_stdio(false);
    std::cout << "a\n";
    std::printf("b\n");
    std::cout << "c\n";
}
like image 33
prosti Avatar answered Oct 13 '22 23:10

prosti