C faster than C++

Lattely, I'm been seeing everywhere that C is faster than C++, and now I'm wondering if that could be true?
Is't it rather the style of coding that's neccesary in C that's makin it faster?

Comments

  • : Lattely, I'm been seeing everywhere that C is faster than C++, and now I'm wondering if that could be true?
    : Is't it rather the style of coding that's neccesary in C that's makin it faster?
    :
    :


    some c++ classes, such as fstreams, that will be correct because c++ fstream can be implemented as a thin wrapper around c functions. Other c++ containers may also run slightly slower than hand-coded equivalent functions in C, but the C functions will be less useful and require recoding for nearly every different data type. In these cases I think the advantage of c++ container more than outweight their slight slowness.

    Otherwise I doubt there is much, if any different, in the speed of c and c++ programs.
  • : : Lattely, I'm been seeing everywhere that C is faster than C++, and now I'm wondering if that could be true?
    : : Is't it rather the style of coding that's neccesary in C that's makin it faster?
    : :
    : :
    :
    :
    : some c++ classes, such as fstreams, that will be correct because c++ fstream can be implemented as a thin wrapper around c functions. Other c++ containers may also run slightly slower than hand-coded equivalent functions in C, but the C functions will be less useful and require recoding for nearly every different data type. In these cases I think the advantage of c++ container more than outweight their slight slowness.
    :
    : Otherwise I doubt there is much, if any different, in the speed of c and c++ programs.
    :
    The speed differences are essentially null on any modern machine. The absolute biggest loss of speed you could probably manage by using C++ versions of C functions is 5%, and that would be pushing it quite a bit. Honsetly it's a matter of which one seems to fit your style more, I've grown to like C more for learning and for coding most programs, simply because I feel I have slightly more control over everything than with C++. Truth be told there is little difference, most compilers today treat C++ as C with different headers and slightly different syntax, which it more or less is. C++ just speeds up the development process in some projects, and most optimizing compilers will negate speed gains from C. Make your choice, which one do you like more, really there is no wrong path here.
  • : : : Lattely, I'm been seeing everywhere that C is faster than C++, and now I'm wondering if that could be true?
    : : : Is't it rather the style of coding that's neccesary in C that's makin it faster?
    : : :
    : : :
    : :
    : :
    : : some c++ classes, such as fstreams, that will be correct because c++ fstream can be implemented as a thin wrapper around c functions. Other c++ containers may also run slightly slower than hand-coded equivalent functions in C, but the C functions will be less useful and require recoding for nearly every different data type. In these cases I think the advantage of c++ container more than outweight their slight slowness.
    : :
    : : Otherwise I doubt there is much, if any different, in the speed of c and c++ programs.
    : :
    : The speed differences are essentially null on any modern machine. The absolute biggest loss of speed you could probably manage by using C++ versions of C functions is 5%, and that would be pushing it quite a bit. Honsetly it's a matter of which one seems to fit your style more, I've grown to like C more for learning and for coding most programs, simply because I feel I have slightly more control over everything than with C++. Truth be told there is little difference, most compilers today treat C++ as C with different headers and slightly different syntax, which it more or less is. C++ just speeds up the development process in some projects, and most optimizing compilers will negate speed gains from C. Make your choice, which one do you like more, really there is no wrong path here.
    :

    I've already choosen path...
    I use C everywhere,except where I want to use some OO, then I use C++...
  • : Lattely, I'm been seeing everywhere that C is faster than C++, and now I'm wondering if that could be true?
    : Is't it rather the style of coding that's neccesary in C that's makin it faster?
    I agree to the responses already given. At http://www.codepedia.com/1/CompareCppAndOtherLanguages I recite an article by a professor (see ref there) which concludes that the variance between programmers of one single language is bigger than the variance between languages. So. it is concluded that C is faster then C++ on average, but there also are many C++ programmers developing faster programs then the average C programmer. It might therefore be wiser to focus on the structure of the algorithms instead of the language (a movement I've noticed in C# development already).

    See ya,
    bilderbikkel

  • :
    : I've already choosen path...
    : I use C everywhere,except where I want to use some OO, then I use C++...
    :
    [blue]
    You have chosen wisely.

    On a serious note, however, the speed of C++ depends on how to use C++. For example, having a lot of overloaded operators and copy constructors may cause some grief, provided you use them without critically reviewing the code. Here is a small example of setting up some properties on some object:[/blue]
    [code]
    SetAttr (_T ("Property1"), _T ("Value1"));
    SetAttr (_T ("Property2"), _T ("Value2"));
    SetAttr (_T ("Property3"), _T ("Value3"));
    SetAttr (_T ("Property4"), _T ("Value4"));
    SetAttr (_T ("Property5"), _T ("Value4"));
    SetAttr (_T ("Property6"), _T ("Value6"));
    SetAttr (_T ("Property7"), _T ("Value7"));
    SetAttr (_T ("Property8"), _T ("Value8"));
    [/code]
    [blue]On the first glance this code does not raise any alarms from a performance point of view. However, if you look at the prototype of that method:[/blue]
    [code]void CMyObject::SetAttr (CString& name, CString& value);
    [/code]
    you can notice that somehow these constant strings are turning into CString objects! Very nice! So, to accomodate that code - compiler will:

    1. Create (read construct :-)) CString on the fly and store it on stack
    2. Pass the ref. to stored CString into 'SetAttr()'
    3. When 'SetAttr()' returns - destroy (read destruct() :-)) that CString

    Now all that is going on for one parameter. If there is a two parameters - then all should be done twice. So, in a single line we have here 2 calls to constructor and 2 calls to destructor of CString. And, if you look at the constructor/destructor of a CString (ATL string in latest versions of VC++) - you will go to depths of code never seen before!

    My point is: C++ is great language, but use it properly. My style in C++ is only pure OOP: inheritance, incapsulation and polymorphism. I do not use any of copy constructing or operators. If I need to do some console I/O - I use good old printf() - I just got used to it.
  • : Lattely, I'm been seeing everywhere that C is faster than C++, and now I'm wondering if that could be true?
    : Is't it rather the style of coding that's neccesary in C that's makin it faster?
    :


    No big difference unless you use generic programming. Things like templates and virtual inheritage will be way slower than the C way, which would be using void-pointers, func-pointers etc. The same code compiled first with a C compiler and then with a C++ compiler should be more or less identical though.

    C++ classes tend to run a load of init crap when you start up the program though, and you can't choose to schedule the constructor calls until later unless you make the variables local. The same reason also makes classes unsuitable for safety-critical systems, unless you have a non-standard option in the compiler.
  • [italic]...virtual inheritage will be way slower than the C way...[/italic]
    [blue]I am not sure about that. Virtual call is only ONE assembly instruction longer that static procedure call! Virtual tables are initialized in static memory at the moment code loads, so no overhead here either. When class instance (object) is created - the virtual table is attached (TWO assembly instructions!). These thing are OK.

    The main problem in C++ is over-engineering the code (using design patterns where they not needed).[/blue]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories