Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

performance overhead of typecasting variables in C [closed]

Tags:

performance

c

x86

I have seen people recommending to avoid typecasting of variables as much as possible in high performing applications. I never understood the reason for this and I tend to typecast lot of times mostly to avoid compiler warning. Does this have any implications on performance?. Commonly seen code:

1) struct X * pass to a function which takes void *

2)uint16_t typecasted to uint32_t

like image 586
deadbeef Avatar asked May 18 '16 17:05

deadbeef


People also ask

Does casting affect performance?

It won't affect program performance, but it will affect development and maintenance performance. Now that Java has generics, there is rarely a need to cast an Object to a more specific type.

Does casting cost performance?

Casting isn't very expensive. Of course, if you have a loop that runs a million times a second it might make sense to avoid casting to save some performance, otherwise it won't really cause performance issues.

Is type casting costly?

Casting is not free, but it isn't that expensive either. There is this answer on stackoverflow which says it's almost free, but keep in mind that it's based on the Microsoft.NET Common Language Runtime - Unity uses different C# implementations in many build targets. Regarding the line GameObject t = c.

Does C support typecasting?

Type Casting is basically a process in C in which we change a variable belonging to one data type to another one. In type casting, the compiler automatically changes one data type to another one depending on what we want the program to do.


1 Answers

Does [casting] have any implications on performance?

Not directly. A type cast expresses an explicit conversion of a value from one data type to another. It is really the conversion that has the potential for a performance impact, and if such a conversion is going to be performed whether you cast or not, then the cast has no performance impact whatever.

For example, some compilers can be set to warn about implicit conversions from floating-point types to integer types, and often these warnings can be silenced by making the conversion explicit with a cast. That does not change the fact that a conversion will be performed, and that such conversions are not free, but the cast doesn't make the conversion any more expensive than it otherwise would be.

Additionally, some conversions can be implemented for free. For example, on most machines, signed and unsigned integer types with of the same width have compatible representations, so converting values between these types is a no-op. Casts that only add or remove type qualifiers other than _Atomic are also in this category.

With regard to your specific examples:

1) struct X * pass to a function which takes void *

C does not require compatible representations for different pointer types, but in practice it is rare these days for different object pointer types to have different representations. Therefore, conversions between pointer types are usually free. That hardly matters, however, because the specific case you ask about is one in which the conversion will be performed whether you insert an explicit cast or not.

2) uint16_t typecasted to uint32_t

This may be free, depending on specific circumstances and compiler implementation. For example, if the value being converted was already being held in a 32-bit register, then it is a no-op. Additionally, the compiler might be able to implement it as a no-op in the specific expression in which it appears. Note also that if uint32_t is the same as unsigned int, as is common, then C semantics require this particular conversion to be performed routinely in the evaluation of arithmetic expressions, so many of these fall into the category of conversions that will happen whether you cast or not.

like image 71
John Bollinger Avatar answered Sep 29 '22 12:09

John Bollinger