Re: Does Casting Slow a Program Down?
- From: "Peter Nilsson" <airia@xxxxxxxxxxx>
- Date: 31 Jan 2007 14:07:37 -0800
On Feb 1, 2:04 am, Chris Dollin <chris.dol...@xxxxxx> wrote:
PeterOut wrote:
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 might, but it would have nothing to do with the casts,
since they're both unnecessary -- `usLimit` is
converted from `unsigned short` to `int` regardless.
No. You'll find there's no shortage of implementations where
unsigned short promotes to unsigned int, not int.
The cast in the assignment is redundant since the conversion
is performed anyway. However, the cast in the condition could
preclude the conversion of i (and usLimit) to unsigned int on
some implementations.
Either way, the code has potential for problems if the limit
is larger than 32767.
--
Peter
.
- References:
- Does Casting Slow a Program Down?
- From: PeterOut
- Re: Does Casting Slow a Program Down?
- From: Chris Dollin
- Does Casting Slow a Program Down?
- Prev by Date: Re: Problem with pointer to function - what's wrong here?
- Next by Date: Re: variable initialization / I'm a n00b
- Previous by thread: Re: Does Casting Slow a Program Down?
- Next by thread: Re: Does Casting Slow a Program Down?
- Index(es):
Relevant Pages
|