Re: Does Casting Slow a Program Down?



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

.



Relevant Pages

  • Re: Problem with linker
    ... but to have actually written a default constructor. ... such as overloading on int and pointer types. ... conversion of 0 to CString requires a user-defined conversion, ... acceleration operator *(distance d, time_squared t2); ...
    (microsoft.public.vc.mfc)
  • Re: pointer
    ... I think you need %02hhx there, or cast pto (unsigned int). ... Technically, yes, but it's also worth pointing out that implementations ... corresponding unsigned type, and vice versa, so for printf not to accept ...
    (comp.lang.c)
  • Re: herding the bits of a float into an integer array
    ... int main ... This cast merely converts the value of x from float to int. ... C casts do the appropriate conversion. ... a pointer to a different type, though in that case the standard ...
    (comp.lang.c)
  • Re: Questions, please
    ... > short is cast'ed to int." ... thing that performs a cast in C is a cast operator. ... cast is an explicit conversion. ... In the case of a function without a prototype, ...
    (comp.lang.c)
  • Re: Interesting list Validity (True/False)
    ... TypeError: int argument required ... If the gmpy writers are not allowing the conversion, ... it raises an OverflowError and the "%d" formatting ...
    (comp.lang.python)