C++ vs C when it comes to speed...

Discussion in 'C++' started by mast2as@yahoo.com, Mar 1, 2007.

  1. Guest

    I am sure this topic has been discussed a thousand times and I read a
    few things about it today on the net. I also want to say I am trying
    to start a polemic here, I am just curious and willint to learn and
    improve the way I am approaching some coding issues that I have at the
    moment. I use C++ programming for my work, but I am not a developper
    so please be patient & tolerant in your answers ;-)

    Okay for the last few days I have been struggling with mixing some C
    and C++ code. I usually try to make my code C++ all the way through.
    Recently because I had to implement some old C specs I decided for
    some weird reasons to mix C and C++ code. Not a good thing to do I
    know. Anyway I really struggled with the idea for the last few days
    and spend quite some time going back & fort different versions of the
    code, some had more C than C++, some where only C++. I must say that
    in what I am doing, execution SPEED is important.

    So today I decided to do this very simple test. I wrote the same
    functionalities but one version is C++ the other C and run these
    functions in a loop (10 000 times).

    The C++ versions takes 5 seconds
    The C version takes 1 second to execute

    This a big difference. I realised that the difference is mostly coming
    in the push_back function of the std::vector class. If I comment that
    line out, the C++ code runs in 1 second. I used to find the STL lib
    very very convenient but I never realised they had such an impact of
    the application performances. Here is the program that I used for the
    test... Maybe I am doing something wrong so I apologize in advance.

    As I said in the pre-ambule of the post, I am trying to be
    constructive. The feedbacks I would like to have are more:
    1/ i am doing something wrong in the C++ implementation that would
    slow it down.
    2/ is it a good thing to do to use C coding in a C++ app if speed is
    an issue and I want the app to run as fast as it could.

    Thanks everyone.

    // 1. comparing speed C vs C++
    // Running on Max OS X, Power PC G4, 1.5 Ghz
    // c++ -o ribparser ribparser.cpp

    #include <stdlib.h>
    #include <stdio.h>

    #include <fstream>
    #include <string>
    #include <vector>

    #include <ctime>

    class RibParser
    {
    std::string m_ribFile;
    public:
    std::ifstream ifs;
    RibParser( std::string ribFile ) : m_ribFile( ribFile )
    {
    char rixm[512];
    try
    {
    ifs.open( ribFile.c_str() );
    if ( ifs.fail() )
    {
    sprintf( rixm, "%s: Can't open\n", ribFile.c_str() );
    throw( rixm );
    }
    }
    catch( char *rixm )
    {
    printf( rixm );
    ifs.close();
    exit( 0 );
    }
    int ch;
    std::vector<char> token;
    while( ! ifs.eof() )
    {
    ch = ifs.get();
    // comment this line out and the app runs in 1 second
    token.push_back( ch );
    }
    }
    ~RibParser()
    {
    ifs.close();
    }
    };

    static const size_t ARRAY_INCR = 8;

    void RibParserC( const char *ribFile )
    {
    char *token;
    size_t tokenByteSize = 0;
    size_t tokenArraySize = ARRAY_INCR;
    FILE *source;
    if ( ( source = fopen( ribFile, "r" ) ) == NULL )
    {
    printf( "%s: Can't open\n", ribFile );
    fclose( source );
    exit( 0 );
    }
    token = (char*)malloc( ARRAY_INCR );
    int ch;
    do
    {
    ch = fgetc( source );
    token[tokenByteSize] = ch;
    tokenByteSize++;
    if ( ( tokenByteSize % tokenArraySize ) == 0 )
    {
    token = (char*)realloc( token, tokenArraySize + ARRAY_INCR );
    tokenArraySize += ARRAY_INCR;
    }
    } while ( ch != EOF );
    fclose( source );
    free( token );
    }

    int main( int argc, char ** argv )
    {
    time_t start, end;
    time( &start );
    for ( size_t i = 0; i < 10000; ++i )
    {
    RibParser ribParser( "./comment.rib" );
    }
    time( &end );
    double diff = difftime( end, start );
    printf( "seconds %f %d\n", diff, CLOCKS_PER_SEC );

    time( &start );
    for ( size_t i = 0; i < 10000; ++i )
    {
    RibParserC( "./comment.rib" );
    }
    time( &end );
    diff = difftime( end, start );
    printf( "seconds %f %d\n", diff, CLOCKS_PER_SEC );

    return 0;
    }

    /////
    , Mar 1, 2007
    #1
    1. Advertising

  2. Guest

    On Mar 1, 2:12 pm, "" <> wrote:
    > I am sure this topic has been discussed a thousand times and I read a
    > few things about it today on the net. I also want to say I am trying
    > to start a polemic here, I am just curious and willint to learn and
    > improve the way I am approaching some coding issues that I have at the
    > moment. I use C++ programming for my work, but I am not a developper
    > so please be patient & tolerant in your answers ;-)
    >
    > Okay for the last few days I have been struggling with mixing some C
    > and C++ code. I usually try to make my code C++ all the way through.
    > Recently because I had to implement some old C specs I decided for
    > some weird reasons to mix C and C++ code. Not a good thing to do I
    > know. Anyway I really struggled with the idea for the last few days
    > and spend quite some time going back & fort different versions of the
    > code, some had more C than C++, some where only C++. I must say that
    > in what I am doing, execution SPEED is important.
    >
    > So today I decided to do this very simple test. I wrote the same
    > functionalities but one version is C++ the other C and run these
    > functions in a loop (10 000 times).
    >
    > The C++ versions takes 5 seconds
    > The C version takes 1 second to execute
    >
    > This a big difference. I realised that the difference is mostly coming
    > in the push_back function of the std::vector class. If I comment that
    > line out, the C++ code runs in 1 second. I used to find the STL lib
    > very very convenient but I never realised they had such an impact of
    > the application performances. Here is the program that I used for the
    > test... Maybe I am doing something wrong so I apologize in advance.
    >
    > As I said in the pre-ambule of the post, I am trying to be
    > constructive. The feedbacks I would like to have are more:
    > 1/ i am doing something wrong in the C++ implementation that would
    > slow it down.
    > 2/ is it a good thing to do to use C coding in a C++ app if speed is
    > an issue and I want the app to run as fast as it could.
    >
    > Thanks everyone.


    to make the C++ test code a closer match to the C code you should add
    token.reserve(ARRAY_INCR);
    just after you define token. This will prevent one (few?) extra new/
    malloc type of calls on push_back.

    Though I doubt this should make such a big difference for this
    particular test. You should also use the optimizer options of the
    compiler for both tests -- C++ code often has a lot of very small
    inline functions that couold slow things down if the compiler made
    them regular functions (as is often the case for fully non-optimzed
    code generation).
    , Mar 1, 2007
    #2
    1. Advertising

  3. Guest

    Thanks for your answer.

    > to make the C++ test code a closer match to the C code you should add
    > token.reserve(ARRAY_INCR);
    > just after you define token. This will prevent one (few?) extra new/
    > malloc type of calls on push_back.


    This change didn't make a big difference indeed.


    > Though I doubt this should make such a big difference for this
    > particular test. You should also use the optimizer options of the
    > compiler for both tests -- C++ code often has a lot of very small
    > inline functions that couold slow things down if the compiler made
    > them regular functions (as is often the case for fully non-optimzed
    > code generation).


    Yees, I tried to compile with the -O2 option and the C++ runs in 1
    second instead of 5 which is big. The returned time for the C version
    is less than 1 second. So the gap is not as important as before, which
    is good. The C version though still runs faster (which is from what i
    have read what you would expect anyway).
    , Mar 1, 2007
    #3
  4. Jerry Coffin Guest

    In article <>,
    says...

    [ ... ]

    > Yees, I tried to compile with the -O2 option and the C++ runs in 1
    > second instead of 5 which is big. The returned time for the C version
    > is less than 1 second. So the gap is not as important as before, which
    > is good. The C version though still runs faster (which is from what i
    > have read what you would expect anyway).


    The C library is normally pre-compiled, with optimization turned on, so
    when you turn off optimization, all that's being left un-optimized is
    the code you write directly.

    Most of the C++ library, including much of the code you used, is written
    as templates. For most practical purposes, templates are not pre-
    compiled at all; instead the source code to the template is typically
    places into the header you've included. This means when you turn off the
    optimizer, a much larger percentage of the final code is being compiled
    without optimization.

    When you consider that, it comes as little surprise that turning
    optimization on or off tends to make a much larger difference in C++
    code than in C code, especially if the C++ code makes much use of
    templates (which includes most of the standard library).

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
    Jerry Coffin, Mar 2, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ham

    I need speed Mr .Net....speed

    Ham, Oct 28, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    2,336
    Antony Baula
    Oct 29, 2004
  2. efiedler
    Replies:
    1
    Views:
    2,030
    Tim Ward
    Oct 9, 2003
  3. Replies:
    2
    Views:
    2,286
    Howard
    Apr 28, 2004
  4. Replies:
    2
    Views:
    332
    Christopher Benson-Manica
    Apr 28, 2004
  5. Replies:
    8
    Views:
    329
    Mike Wahler
    Mar 3, 2007
Loading...

Share This Page