C++ or C for performance applications?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: C++ or C for performance applications?

  1. #1
    Join Date
    Dec 2002
    Location
    La Plata, Buenos Aires
    Posts
    615

    C++ or C for performance applications?

    Ive read in some sites that if you add C++ features to a C program, for example, replacing <stdio.h> with <iostream> makes the program slower due to the overhead of the C++ standard library.

    Also, many sites propose C as the language for raw performance applications.

    Its true that C++ is "slower" than C?
    Its good programming practice to mix C and C++ features in a program (and of course, using the CPP compiler)?

    thank you
    youre great guys

  2. #2
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443
    If I remember correctly, it was Bjarne Strostrup who responded to a question very simmilar to yours. The question was something along the lines "is it true that in C++ you are not so close to the machine as you are in C?". I don't have that material at hand, but the answer was saying that, basically yes, that is true. C++ is more complex than C and enable you to write code on more abstract level. This makes it harder to "guess" where the execution bottle-necks lie and what the runtime impact of certain constructs is.

    C++ is (roughly) a superset of the C programming language. You can do in C++ everything you can do in C (I am not referring to the syntax here). C++ is a multiparadigm programming language, combining OOP with procedural programming and template based programming. It is a question of design which paradigm you apply to model your problem.

    As a direct response to your questions, the widely spread idea that using the standard C++ library slows down your code is wrong. Incorrect use of the standard library can slow down your code. The same is true for C++ versus C: C++ is not slower than C by definition or something. It is so that the incorrect use of C++ language facilities may have a catastrophical impact on the program performance.

    Again, Bjarne once said that (quoting from memory) "it is harder to shoot in your foot in C++ than in C, but if you do, your entire leg is blown away"
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  3. #3
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Originally posted by Gabriel Fleseriu
    Again, Bjarne once said that (quoting from memory) "it is harder to shoot in your foot in C++ than in C, but if you do, your entire leg is blown away"
    I love this one...
    Ciao, Andreas

    "Software is like sex, it's better when it's free." - Linus Torvalds


    Article(s): Allocators (STL) Function Objects (STL)

  4. #4
    Join Date
    Dec 2002
    Location
    La Plata, Buenos Aires
    Posts
    615
    so I must deveop in C, C++ or a mix of both? I think i should learn deeply both languages.

  5. #5
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443
    I would go with C++. If you can ride a motorbike, you can certainly ride a bike, too
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  6. #6
    Join Date
    Apr 2002
    Posts
    8
    I have developed many programs in both C and C++ languages. Clearly I never noticed that C++ is slower than C in anyway.

    In fact I have written a performance tester to test which one is faster, C or C++ by writing a simple class and methods in C++, and a function in C. To my astonishment I have found that C++ code is faster than code written in C!

    Why? Because today all the new compilers are written with C++ in mind and not C. Therefore, they optimize C++ code and do not care to optimize C code. Final result is that if you write a program in C++ it will perform better than the same written in C.

    If you want to choose between whether to learn C or C++, my advise is go for C++. Not because it is faster, but if you learn C++ then it will be easy for you to think in object oriented terms. Otherwise if you learn C first, it is very difficult to migrate to the object oriented mindset.

  7. #7
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557

    Use proper development techniques.

    indio,

    This is a rich topic indeed. In our group, we are primarily involved in the creation of real-time control software for embedded microcontroller systems. We write everything including the operating system, hardware drivers, as well as all the high-level application software.

    Assuming that the development team has complete mastery of the C++ language elements and the design and optimization goals, there is no compelling reason to prefer C over C++,

    Remember that different projects and different modules within each project have different design goals. There are several types of programming optimizations which can generally be classified within one of three groups:

    1) Speed-optimized
    2) Memory (code-size) optimized
    3) Optimal portability

    Most program elements will need to incorporate all of these three kinds of optimizations. Generally, the high-level application software will emphasize portability and size over speed, whereas the low-level driver software is often purely speed-optimized. Truly speed-critical sequences can usually be identified and optimized properly. We use code-profiling or real-time emulation systems for performance analysis.

    The nice thing about C++ is that the language is rich enough to allow for portability and design elegance, yet still offers all the low-level manipulations of pure C.

    Again, this is a rich topic with no clear right or wrong answer.

    Chris.

    You're gonna go blind staring into that box all day.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,418
    Originally posted by indiocolifa
    so I must deveop in C, C++ or a mix of both? I think i should learn deeply both languages.
    Either learn 'C' or learn C++. Don't waste time and make the mistake of learning 'C' with the intention of learning C++. You'll be doing yourself a disservice since there will be things you will have to "unlearn" when going from 'C' to C++.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Feb 2003
    Posts
    53
    Originally posted by Paul McKenzie
    Either learn 'C' or learn C++. Don't waste time and make the mistake of learning 'C' with the intention of learning C++. You'll be doing yourself a disservice since there will be things you will have to "unlearn" when going from 'C' to C++.

    Regards,

    Paul McKenzie
    ^ gold!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center