Re: Does Casting Slow a Program Down?
- From: "David T. Ashley" <dta@xxxxxxxx>
- Date: Wed, 31 Jan 2007 13:50:45 -0500
"PeterOut" <MajorSetback@xxxxxxxxxx> wrote in message
news:1170255047.573548.183190@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
If I had code like this.
unsigned short usLimit=10
int a[10], i;
for (i=0; i<(int)usLimit; ++i)
{
a[i]=(int)usLimit;
}
would it run slower than this?
int a[10], i, iLimit=10;
for (i=0; i<iLimit; ++i)
{
a[i]=iLimit;
}
It usually depends on the compiler. Compilers vary greatly in the quality
of the optimizations they can perform.
However, in this particular case, the answer would usually be "NO". I have
not seen a compiler so poor that it would treat the two cases you gave
differently.
I would expect more variability in a case like this:
int x,y;
x = 10;
y = (char)x;
A clever compiler will recognize that with x==10, the cast has no effect. A
less clever compiler would actually truncate to a character than convert
back to an int.
In your example, most compilers will realize that the cast has no effect.
("Realize" is an inappropriate anthropomorphization of the compiler -- more
precisely, the expression trees it builds and the algorithms it applies ot
the trees will weed this out.)
--
David T. Ashley (dta@xxxxxxxx)
http://www.e3ft.com (Consulting Home Page)
http://www.dtashley.com (Personal Home Page)
http://gpl.e3ft.com (GPL Publications and Projects)
.
- References:
- Does Casting Slow a Program Down?
- From: PeterOut
- Does Casting Slow a Program Down?
- Prev by Date: Re: variable initialization / I'm a n00b
- Next by Date: Re: variable initialization / I'm a n00b
- Previous by thread: Re: Does Casting Slow a Program Down?
- Next by thread: fseek and open_memstream
- Index(es):
Relevant Pages
|