Re: Does Casting Slow a Program Down?
- From: Zara <me_zara@xxxxxxxxxxxxxxx>
- Date: Wed, 31 Jan 2007 16:01:54 +0100
On 31 Jan 2007 06:50:47 -0800, "PeterOut" <MajorSetback@xxxxxxxxxx>
wrote:
If I had code like this.
unsigned short usLimit=10
<<<<< Missing semicolon
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 depends on the compiler, but the answer should usually be 'No'
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;
Best regards,
Zara
.
- Follow-Ups:
- Re: Does Casting Slow a Program Down?
- From: PeterOut
- Re: Does Casting Slow a Program Down?
- References:
- Does Casting Slow a Program Down?
- From: PeterOut
- Does Casting Slow a Program Down?
- Prev by Date: Re: How to reduce Zero Initialised region.
- Next by Date: Re: Does Casting Slow a Program Down?
- Previous by thread: Does Casting Slow a Program Down?
- Next by thread: Re: Does Casting Slow a Program Down?
- Index(es):
Relevant Pages
|