views:

283

answers:

4

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?).

A: 

This depends on the compiler used. For example, if you compile a C program with a C++ compiler it runs the same.

+1  A: 

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.

Alex B
+2  A: 

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.

paxdiablo
Thanks for your answer, but i want to know if c++ do something additionally which we don't need, and how it affects the performance (makes it slower? or nothing?). for example, RAII does what that makes C++ slower? (or may not) and other things that may C++ does to make it slower than C (or may not).
PC2st
@PC2st, if you want to know that, then test that. It will depend on the implementation. I could quite easily write a C compiler that generates code slower than g++ (that's actually likely since, based on the assembler output, the g++ guys are much more knowledgeable about optimisation than I). Crikey, I could write a C compiler whose code ran slower than GWBASIC :-) Unless you start comparing _specific implementations_ (so that actual data can be gathered rather than people's anecdotes), the question seems meaningless.
paxdiablo
+6  A: 

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.

Ugo
If we don't use pointers, such as `m` variable in the above example, C++ uses RAII to allocate and free its memory (when an error occurred in the constructor, the allocated memory will be freed) (and if an error is occurred after class initialization, the destructor will be called automatically).
PC2st
@PC2st Just because it works LIKE RAII does not mean it IS RAII.
SoapBox
@PC2st When you say if an error occurs, you mean if an exception is thrown right ? It means that you're using an extra feature you wouldn't have used in C. If you want the comparison to be meaningful you must either do not use exception, either implement exception handling in C.
Ugo