Is it worth writing a piece of code in C instead of C ++ as micro-optimization?

I am wondering if it is still worth using modern compilers and their optimization to write critical code in C instead of C ++ to make it faster.

I know that C ++ can lead to poor performance when classes are copied, when they can be passed by reference, or when classes are created automatically by the compiler, usually with overloaded operators and many other similar cases; but for a good C ++ developer who knows how to avoid all this, is it still worth writing C code to improve performance?

+7
c ++ performance c micro-optimization
source share
5 answers

I agree with a lot of comments. The C syntax is supported, intentionally (with a divergence only in C99), in C ++. Therefore, all C ++ compilers must support it. In fact, it seems to me that it is difficult to find any dedicated C compilers. For example, in GCC you will actually use the same optimization / compilation mechanism, regardless of whether the code is C or C ++.

The real question is whether simple C code is being written and C ++ compilation suffers from performance degradation. The answer, in fact, is no. There are a few tricky notes about exceptions and RTTI, but these are mostly resizing, not speed. It will be so hard for you to find an example that really strikes a performance hit that doesn't seem to be worth writing a dedicated module.

What has been said about what features you use is important. In C ++, it is very easy to get sloppy regarding copy semantics and get huge overhead from copying memory. In my experience, this is the biggest cost - in C you can also suffer from this cost, but not so easily, I would say.

Virtual function calls are always slightly more expensive than regular functions. At the same time, forced built-in functions are cheaper than regular function calls. In both cases, this is most likely the cost of pushing / popping parameters from the stack, which is more expensive. Concern about the overhead of functions, although it should be quite late in the optimization process - as this is rarely a significant problem.

Exceptions are expensive during a throw (at least in the GCC). But setting up catch statements and using RAII does not have significant costs associated with this. This was developed by the GCC compiler (and others), so really only exceptional cases are expensive.

But to summarize: a good C ++ programmer cannot make his code faster just by writing it in C.

+18
source share

measure! measure before thinking about optimization, measure before applying optimization, measure after applying optimization, measure!

If you have to run your code 1 nanosecond faster (because it will be used by 1000 people, 1000 times in the next 1000 days, and which second one is very important), everything will be.

Yes! it costs...

  • change of languages ​​(from C ++ to C; from Python to COBOL, from Mathlab to Fortran, from PHP to Lisp)
  • compiler settings (enable / disable all -f options)
  • use different libraries (even write yourself)
  • etc.
  • etc.

What you must not forget is measure! .

+10
source share

pmg nailed it. Just measure instead of global assumptions. Also think about it this way, compilers like gcc share front, middle and end. therefore frontend fortran, c, c ++, ada etc. ends in the same inner middle language if you do this, which does most of the optimization. Then this common middle language is turned into assembler for a specific purpose, and targeted specific optimizations arise. Thus, a language may or may not call more code from the beginning to the middle, when the languages ​​are very different, but for C / C ++, I would suggest that it is the same or very similar. Now, binary size is another story, libraries that can be pulled into binary for C only vs C ++, even if it can only be C syntax. It does not necessarily affect the performance of the execution, but can accumulate the amount of data storage and transmission in the program files, as well as memory requirements, if the program is loaded after a while into the RAM. Here again, just measure.

I also add compiler assembly and / or disassemble the result to the measure comment and compare the results of your different languages ​​/ compiler options. This can / will complement the time differences that you see when measuring.

+1
source share

The question answered death, so I will not add to this.

Just as a general question, assuming you measured, etc., and you determined that a particular segment of C ++ code (or another) is not working at optimal speed (which usually means that you did not use the right tool for the job) ; and you know that you can get better performance by writing it in C, then yes, it’s definitely worth it.

There is a certain way of thinking that is common, trying to do everything from one tool (Java or SQL or C ++). Not only Maslow Hammer, but also the actual belief that they can code the C construct in Java, etc. This leads to all kinds of performance problems. Architecture, like a real profession, is about placing code segments in an appropriate architectural place or platform. This is the right combination of Java, SQL, and C for performance. This creates an application that does not need to be visited again; unprecedented performance. In this case, it does not matter if C ++ implements these constructors or not.

+1
source share

I am wondering if it is still worth using modern compilers and their optimization to write critical code in C instead of C ++ to make it faster.

not. keep it readable. if your team prefers C ++ or c, prefer this, especially if it already works in production code (do not rewrite it for no particular reason).

I know that C ++ can lead to poor performance if classes are copied when they can be passed by reference

then prohibit copying and assignment

or when classes are created automatically by the compiler, usually with overloaded operators and many other similar cases

Could you clarify? if you refer to templates, they have no additional cost at runtime (although they can lead to additional exported characters, which will lead to an increase in the binary file). in fact, using the template method can improve performance if (for example) conversion would otherwise be necessary.

but for a good C ++ developer who knows how to avoid all this, is it still worth writing C code to improve performance?

in my experience, a C ++ expert developer can create a faster and more user-friendly program.

You must be selective about the language features you use (and not use). if you violate C ++ functions before the set available in c (for example, remove exceptions, virtual function calls, rtti), you can start a good start. if you learn to use templates, metaprogramming, optimization methods, avoid type aliases (which gets harder or harder in c), etc., then you should be at or faster than c with a program that is more easily supported ( since you are familiar with C ++).

if you are comfortable using C ++ functions, use C ++. it has many features (many of which are added at speed / cost), and they can be written as fast as c (or faster).

With templates and metaprogramming, you can turn many variables at runtime into compile-time constants for exceptional achievements. sometimes it spreads well to micro-optimization territory.

0
source share

All Articles