Re: Does Casting Slow a Program Down?



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
.



Relevant Pages

  • Re: How do I dig a memory value out?
    ... The only C compiler I could find at the time was ... The Atari keyboard was missing several characters, ... higher level language plus a few common data types ... hand tweeked machine code was entered directly in the ...
    (microsoft.public.vb.general.discussion)
  • Re: Basic compiler
    ... stand-alone executables in machine code were the "Einstein Compiler" ... and Microsoft's TASC (The AppleSoft Compiler). ...
    (comp.sys.apple2)
  • Re: Basic compiler
    ... stand-alone executables in machine code were the "Einstein Compiler" ... and Microsoft's TASC (The AppleSoft Compiler). ... Apple once made an effort to use the "R" file type in Apple ...
    (comp.sys.apple2)
  • Re: vm02 update
    ... Instant Pascal was a misguided attempt at creating an IDE with automatic formatting and almost instant feedback. ... Editing the source in a tokenized format would keep the memory constraints lower and ease the first pass of the compiler. ... compiled to machine code at all. ...
    (comp.sys.apple2.programmer)
  • Re: VB Com compatibility
    ... C/C++ components are generally going to be faster, ... |> interpreted) while VC is compiled directly to machine code. ... | compiled to native code as well as P-Code. ... | "Visual Basic 5 uses the same compiler technology as Visual C++" ...
    (microsoft.public.vb.com)