C ++, which is slower than its C equivalent?

Are there any aspects of the C ++ programming language where it is known that code is slower than the equivalent C language? Obviously, this would exclude OO functions such as virtual functions and vtable functions, etc.

I am wondering when you are programming in a critical area (and you are not concerned about OO functions), can you stick with basic C ++ or better C?

+4
source share
7 answers

Nothing in the standards of the C or C ++ language determines the speed of any construction (C ++ indicates the time complexity of some operations applied to containers, but without going beyond your question). The speed of the code generated for this construction depends on the compiler used to compile it and the system on which it is executed.

For a given code construct, which is real C and real C ++ with the same semantics, there is no fundamental reason why it should be faster than another. But most likely it will be faster than others if the compiler developers are a little smarter.

+5
source

It depends on what you mean by "equivalent." If you are comparing C stdio.h with C ++ iostream, stdio.h operations are usually faster (in some situations this happens pretty quickly). But if you are talking about code written in a subset of C ++ that also compiles as valid C, the generated machine code is likely to be identical.

+3
source

In one C ++ example, the restrict keyword is missing. Used correctly, which sometimes allows the compiler to create faster code.

  • in practice, it’s quite rare to see the benefits of restrict , but this happens,
  • There are many cases where the C ++ or C compiler can infer (after embedding) that the necessary conditions for restrict apply, and act accordingly even if the keyword is not used,
  • C ++ compilers, which are also C compilers, can provide restrict or __restrict as an extension anyway.

But sometimes (or quite often in some domains) restrict is a really good optimization, which I am talking about is one of the reasons for using Fortran. This, of course, is one of the reasons for strict alias rules in both C and C ++, which offer the same optimization opportunity as restrict for a more limited set of conditions.

If you “think,” it depends on what you think is the “equivalent code.” restrict never changes the value of the program that uses it - compilers can ignore it. Therefore, it is not necessary to describe a program that uses it (for the eyes of the C compiler) and a program that does not execute (for C ++) as "equivalent." The restrict version took more (perhaps only a little more) the programmer’s efforts to create, since the programmer must be sure that he will fix it before using it.

If you mean if there is a program that is valid C and also valid C ++ and has the same meaning for both, but the implementations are somehow limited by the C ++ standard to run it slower than the C implementations, then I'm pretty sure the answer is no. If you mean if there are any potential performance improvements available in the C standard, but not in the standard C ++, then the answer is yes.

If you can get any benefit from customization, it’s another matter: will you get more benefit for the same amount of effort with a different optimization available in both languages, it’s different, and will there be any benefit big enough to base your choice of language on -still remains different. It is ridiculously easy to interact between C and C ++ code, so if you have any reason to prefer C ++, then, like any optimization that changes your preferred coding method, switching to C will usually be something what you do when your profiler says you are too slow, not before.

Also, I'm trying to convince myself, one way or another, whether the performance for exceptions depends, assuming you never use any type that has a non-trivial destructor. I suspect that in practice this is possible (and that this contradicts the principle of "do not pay for what you do not use"), if only because otherwise there would be no gcc point with -fno-exceptions . C ++ implementations reduce the cost quite low (and this is mainly in rhodates, not in the code), but this does not mean that it is equal to zero. Time-critical critical code may or may not be critical for binary code.

Again, this may depend on what you mean by "equivalent" code - if I need to compile my so-called "standard C ++ program" using a non-standard compiler (for example, g++ -fno-exceptions ) to "prove", Since C ++ code is as good as C, in a sense, the C ++ standard is worth something to me.

Finally, the C ++ runtime environment itself has an initial cost that is not necessarily identical to the initial cost of starting C for the "same" program. You can hack altogether in order to lower the cost by removing things that you don’t need strictly. But this effort, the implementation does not necessarily do this for you completely every time, therefore it is not entirely true that in C ++ you are not paying for what you are not using. That is a general principle, but its achievement is a problem of implementation quality.

+3
source

Contrary to the fact that many C programmers like to think, it is often possible to write more rigorous and faster C ++ code without sacrificing much demand for design.

I can’t think of anything in C ++, which is slower than its counterpart in C. Virtual functions are NOT excluded.

+2
source

Does the value of the critical value mean as quickly as possible, or simply means that everything should work in a predictable time?

If this is the second case, then the only things that don't happen at the predictable time are new , delete and try/catch . In embedded programming, there are directives to avoid such calls. In other cases, especially in C ++ 11, you may notice that some things are faster (std :: sort is faster than C sort ()), and some are slower.

But what you get compared to C ++ is higher levels of abstraction, and you don't get C-style errors (e.g. typical malloc() without the corresponding free() )

+1
source

In C ++, there is no code whose direct equivalent in C is faster or, indeed, more convenient.

0
source

If you use a language that crosses C and C ++, then any performance difference when compiling this code with the C compiler compared to the C ++ compiler is purely an implementation quality problem; there is no reason why you need to be faster than another.

Of course, in the real world, if you use C ++, you program using both the additional functions that C ++ has over the intersection of C and C ++, and the use of different idioms than you would in C. And vice versa, if you're using C, you at least use very different idioms from what you would use in C ++, and you can also use the advanced features of the modern C language that C ++ does not have, for example, as Steve said, The restrict or VLA keyword is either pointer types to VLA, or compound literals. In some situations, these functions can bring great performance benefits, but in my experience, they are not the reasons why most people who choose C over C ++ do so.

In my opinion, the main difference is idioms. In C, it is idiomatic to create a linked list by placing the next / prev pointers directly into the structure stored in the list, instead of using a separate wrapper for the list and list nodes. In C, it is idiomatic for iterating directly over the elements of a string as an array of characters. In C, it is idiomatic for working with objects that exist entirely in automatic storage when possible, and not for distributing dynamic storage. And so on. These idiomatic differences are the main ways that C code tends to be faster and easier than C ++.

Of course, if you want, you can use the same idioms in C ++, since most of the language constructs needed for most C-idioms exist at the intersection of C and C ++. And then you can also use the advanced features of C ++ when necessary. But you will write non-idiomatic C ++ code, and you can get a lot of criticism from other C ++ programmers ...

0
source

Source: https://habr.com/ru/post/1433666/


All Articles