What code compiles C and C ++ works correctly, but it may differ in speed due to differences in C and C ++ standards?

Example: malloc(sizeof('x') * 100500) will allocate in sizeof(int) times more memory in C, which theoretically could slow down the program.

  • 2
    it may differ if you have different compilers and compilation options. - KoVadim
  • why minus? - ParanoidPanda
  • @KoVadim, I ask about the differences in standards, and not about the implementation of specific compilers. - Abyx
  • with and with ++ two, albeit slightly similar, but quite different languages. And their standards are quite different. Therefore, this comparison is a bit meaningless. - KoVadim
  • one
    There is a topic on enSO about differences in the behavior of the same code. The question of speed can be assessed in each case. But it can sometimes be meaningless, because in fact, although the code will look the same, there will be several different things to do. - αλεχολυτ

2 answers 2

If to abstract from questions of quality of implementation of compilers C vs. With ++, the same code in C and C ++ can differ in speed only if it differs in semantics. That is, it will only be “the same code” in such a situation from a purely cosmetically-visual point of view.

You can roughly go to the differences in the rules of the name lookup

 struct S { int a[10]; }; int main() { struct Local { struct S { int a[10000]; } s; }; struct S s = { 0 }; // `sizeof s`??? } 

In the C language, a “big” object is declared and reset, and in C ++, a “small” object is declared. But this is too rough.

The property of the C ++ language to carefully preserve the lvalue of the results of expressions will cause the loop

 for (unsigned n = sizeof (1 ? "Hello" : "World"); n > 0; --n) ; 

different number of times in C and in C ++ will be executed. This is also a rather rude reception.

Further worth mentioning the following

 const int N = 10; int A[N][N][N]; 

In C ++, this is a regular array, and in C, it is a Variable Length Array with potential overhead at runtime.

Continuing the VLA theme, the operand of such a sizeof in C is a computable expression (meaning the above declaration A )

 unsigned n = sizeof A[rand() % N][rand() % N]; 

And although, according to the C language specification, the calculation of index-expressions is not required in this case (because the result does not depend on them), the compiler has the right to calculate them (GCC, which is interesting, calculates). In C ++, the sizeof operand is never computable; such sizeof calculated at compile time and does not generate executable code.

A heap of trifles can be hidden in the differences of the rules sequencing these languages. The same property of preserving the lvalue-ness of the results of expressions in C ++ leads to the fact that its rules are more strict and restrictive sequencing than analogous rules in C. This could theoretically lead to more efficient code in C. it gave a tangible effect offhand is not easy.)

The potential need for exception handling in C ++ and stack promotion is another reason why almost any identical C and C ++ code can be interpreted less efficiently in C ++.

  • Can I talk about the cycle in more detail? - αλεχολυτ
  • 3
    @alexolut: In C ++, since both operands of the operator ?: are lvalues ​​and have the same type ( const char [6] ), the loss of lvalue in the operator ?: does not occur, i.e. the result of such an operator ?: in C ++ has the type const char [6] and is an lvalue. As a result, sizeof gives the size of the array - 6. In the C language, which has the property to convert lvalue to rvalue immediately and without hesitation, the type "array" will immediately be converted to type "pointer" ( char * ). The result of the ?: Operator is of type char * and sizeof will give a pointer size. - AnT

Theoretically, fabs ((char) x) and cmath counterparts that have overloads for integer types can work faster. Appeared in C ++ 11, if I'm not mistaken.

  • one
    In C there is a <tgmath.h> that achieves virtually the same effect. - AnT