Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my C++ app faster than my C app (using the same library) on a Core i7

I have a library written in C and I have 2 applications written in C++ and C. This library is a communication library, so one of the API calls looks like this:

int source_send( source_t* source, const char* data );

In the C app the code does something like this:

source_t* source = source_create();
for( int i = 0; i < count; ++i )
    source_send( source, "test" );

Where as the C++ app does this:

struct Source
{
    Source()
    {
        _source = source_create();
    }

    bool send( const std::string& data )
    {
        source_send( _source, data.c_str() );
    }

    source_t* _source;
};

int main()
{
    Source* source = new Source();
    for( int i = 0; i < count; ++i )
        source->send( "test" );
}

On a Intel Core i7 the C++ code produces almost exactly 50% more messages per second.. Whereas on a Intel Core 2 Duo it produces almost exactly the same amount of messages per second. ( The core i7 has 4 cores with 2 processing threads each )

I am curious what kind of magic the hardware performs to pull this off. I have some theories but I thought I would get a real answer :)

Edit: Additional information from comments

Compiler is visual C++, so this is a windows box (both of them)

The implementation of the communication library creates a new thread to send messages on. The source_create is what creates this thread.

like image 535
Charles Avatar asked Jan 13 '10 04:01

Charles


People also ask

How much faster is C 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. The difference is between C idioms and C++ idioms.

What makes C++ so fast?

C++ performance. In contrast, a program written in C++ gets compiled directly into machine code -- without an intermediary translation required at runtime. This is one reason why C++ programs tend to perform faster than those written in Java.


2 Answers

From examining your source code alone, I can't see any reason why the C++ code should be faster.

The next thing I would do is check out the assembly code that is being generated. If you are using a GNU toolchain, you have a couple of ways to do that.

You can ask gcc and g++ to output the assembly code via the -S command line argument. Make sure that other then adding that argument, you use the exact same command line arguments that you do for a regular compile.

A second option is to load your program with gdb and use the disas command.

Good luck.

Update

You can do the same things with the Microsoft Toolchain.

To get the compiler to output assembly, you can use either /FA or /FAs. The first should output assembly only while the second will mix assembly and source (which should make it easier to follow).

As for using the debugger, once you have the debugger started in Visual Studio, navigate to "Debug | Windows | Disassembly" (verified on Visual Studio 2005, other versions may vary).

like image 197
R Samuel Klatchko Avatar answered Sep 20 '22 02:09

R Samuel Klatchko


Without seeing the full code or the assembly my best guess is that the c++ compiler is inlining for you. One of the beauties of c++ compilers is the ability to inline just about anything for speed, and microsoft's compilers are well known to gratuitously inline almost to the point of unreasonably bloating end executables.

like image 28
Beanz Avatar answered Sep 21 '22 02:09

Beanz