Re: Does Casting Slow a Program Down?
- From: "santosh" <santosh.k83@xxxxxxxxx>
- Date: 31 Jan 2007 08:01:48 -0800
PeterOut wrote:
On Jan 31, 10:01 am, Zara <me_z...@xxxxxxxxxxxxxxx> wrote:
If the compiler detectes tha usLimit and iLimit are constants, then it
may detect that (int)usLimit is also constant and it would probably
generate the exact same machine code for both samples.
But this behaviour depends on the optimizations capabilities an
settings of the compiler, and of the code surrounding the snippet you
have shown us.
You may help the compiler declaring either
const unsigned short usLimit=10;
or
const int iLimit=10;
Thanks very much for your fast reply. I was thinking in general
terms. usLimit/iLimit are given a constant value in the example that
I posted but I was thinking in terms of their being variables that are
determined by input data and/or passed as arguments to a function.
Also, what about casting a (single precision) float to a double or
vice versa? Would that impact the run time or would it depend on the
compiler?
Casting is an operation in C. It may or may not translate to actual
hardware instructions, depending on the size and bit representation of
the various types. Optimisers can also effect the final object code.
So, the only way to find out if casting is incurring runtime penalty,
on a particular implementation, compiled under a particular
optimisation level, is to test and compare the results. On most modern
systems and compilers, it unlikely to make a difference.
It more important to ask yourself if the cast is really needed, rather
than it's runtime performance.
.
- References:
- Does Casting Slow a Program Down?
- From: PeterOut
- Re: Does Casting Slow a Program Down?
- From: Zara
- Re: Does Casting Slow a Program Down?
- From: PeterOut
- Does Casting Slow a Program Down?
- Prev by Date: Re: check if the memory is low, from Windows to Unix
- Next by Date: fseek and open_memstream
- Previous by thread: Re: Does Casting Slow a Program Down?
- Next by thread: Re: Does Casting Slow a Program Down?
- Index(es):
Relevant Pages
|