Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a C++ program really slower than a similar C program? [closed]

Assume that i have written a program in C++ without using RTTI and run-time polymorphism (no virtual function, no virtual inheritance) and classes don't have private/protected members, also the C++ specific header files are not used (i.e. C header files are used: cstring, cstdio, ... instead of string, iostream, ...).

Then i want to write a similar program in C which the first argument type of the functions are corresponded to the related struct.

For example:

//C++ code

struct Custom
{
    int a;
    Custom() { }
    void change() { }
    ~Custom() { }
};

int main()
{
    Custom m; //init m
    m.change();
    //destroy m
}

/*C code*/

struct Custom
{
    int a;
};
void custom_init(Custom* like_this) { }
void custom_change(Custom* like_this) { }
void custom_destroy(Custom* like_this) { }

int main()
{
    Custom m;
    custom_init(&m);
    custom_change(&m);
    custom_destroy(&m);
}

Is the C++ program slower than the similar C program (Generally)? if yes, why C programs are faster then? I know, C++ uses the RAII design pattern for memory management, is it the reason for the slow?

I heard that some people said the C programs is faster... why?

Edit: Why this question is closed? i wanted to know if c++ does something additionally which we don't need, and how it affects the performance (makes it slower? faster? or nothing?).

like image 704
Sadeq Avatar asked Aug 15 '10 12:08

Sadeq


People also ask

How much slower is C++ to C?

Hyperfine tells me that the C++ program relying on the dynamically loaded C++ library takes almost 1 ms more time than the C program.

Is C++ slower than C Why or why not?

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 C# really slower than C++?

C++ code is much faster than C# code, which makes it a better solution for applications where performance is important. For instance, your network analysis software might need some C++ code, but performance is probably not a huge issue for a standard word processing application coded in C#.

How many times slower is C# than C++?

It's quite surprising that there is a 20 x difference in the streaming performance. While C# code is unlikely to outperform the same C++ code, in some scenarios it can be very fast.


3 Answers

C++ doesn't use RAII. You CAN use RAII in your c++ program.
As long as you are doing exactly the same thing in C++ and in C, both program should be exactly as fast.
Writing fast programs in C or C++ is not a matter of programming language but of what kind of feature you use.

like image 199
log0 Avatar answered Sep 26 '22 02:09

log0


You'll never know until you try. If the C++ uses anything that C does not (such as constructors, destructors and even non-virtual methods), it may be slower.

But the difference will probably be so small as to be unnoticeable.

Early implementations of C++ may have been slower than C but that's the nature of any software. It improves with time.

Measure, don't guess! Profile your specific code to see which is faster. But even if C code is faster, the price of losing all that extra functionality may be too much. Execution speed is only one speed, and rarely the important one. My opinion of which speed is the most important is development speed.

like image 34
paxdiablo Avatar answered Sep 23 '22 02:09

paxdiablo


No, it's almost certainly false. RAII by itself will not make the program slower. Both C and C++ compilers would probably generate almost identical code for these examples.

like image 30
Alex B Avatar answered Sep 27 '22 02:09

Alex B