C style type casting impact on program perf.

Nov 17, 2011 at 2:39pm

Hi,

I am wondering if a C style type casting has an impact on program performance or anything?

I know casting is done at compile time and thus it should not have any impact, but I am looking for more info on this.

Thank you for your help.
Nov 17, 2011 at 3:01pm
For basic types, not unless you're converting between integer and floating point types, which are relatively expensive operations.
For complex types, a cast is either invalid, or involves a function call to the conversion operator overload. For a valid cast performed with the syntax T(x) rather than (T)x, a constructor is called.
Nov 17, 2011 at 3:13pm
Sorry, I should have given an example. Here it is,

float f;
f = 3.0;

int i; i = (int) f;

How does this impacts the program performance? Does it?

Thanks, Bojan
Nov 17, 2011 at 3:25pm
Yes, it does.
Nov 17, 2011 at 4:47pm
Hi ,
how do we decide which cast is optimize the performance , is there any thumb rule ?
Nov 17, 2011 at 5:07pm
There's no difference for basic types.
For complex types, it depends on the implementations of the various functions. For example, for an x of type R, in principle the implementations of T::T(const R &) and R::operator T() could differ significantly such that T(x) and (T)x take very different amounts of time, but this would be rare in practice.
Nov 17, 2011 at 5:14pm
thanks helios .. that is the thing to remember . thanks a lot
Topic archived. No new replies allowed.