This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

performance difference between int and short



in C66, int is 32 bits and short is 16 bits.

For most of local variables, including loop counter, we know that it won't exceed 16 bits.

Is there any difference in performance between using int and short for such cases? I can understand that int always cost more in cache, how about other aspect, such as compilation optimization?

thanks

Weichun

  • Hi Weichun,

    it's better to use 32-bit for loop counters because the register width is 32-bit ....

    Kind regards,

    one and zero

  • I'm going to move this over to the compiler forum.  One & Zero's comment regarding the counter is correct.  And I've always left it as just plan INT for local variables unless I need to perform some processing with it w/ data of a lower data type that would take advantage of specialty instructions (such as DOTP4 instructions) that said, the compiler folks would probably have much better insight as to if there's anything that's going to give you better performance from doing this other than taking advantage of specialty instructions for data processing.

    Best Regards,

    Chad

  • On C66x you should always use INT for loop counters if possible. There can be significant performance improvements in comparison with using short (I have seen up to 20 % for relatively simple loops).

    Also, it is better to use INT rather than UNSIGNED INT. Using UNSIGNED INT in loop counters may hurt performance in some cases because the compiler needs to take into account potential wrap-around of the counter.

    -Matti

  • Matti,

    Can you explain better why it's better to use INT instead of UINT?

    In section 3.2 of this document it http://www.ti.com/lit/an/spra666/spra666.pdf says that it's better to use INT also as index variables used for subscripting, but I didn't understand why.

    Thanks

  • Unsigned types in C can wrap.  The compiler must take into account that ui+1 is not greater than ui, which can complicate efforts to transform loops into simpler faster forms.

    Signed types effectively can't wrap, because doing so involves a signed overflow whose behavior is undefined or implementation-determined, and won't occur in a correct program.  Thus the compiler can believe that i+1 is always greater than i.

    The difference between signed and unsigned isn't a direct effect -- one is not inherently more efficient than the other -- it's an indirect effect in that more beneficial things can be done with signed loop variables.

  • I agree with the advice given so far.  Note that the C standard suggests that the size of  "int" for each particular machine is the "natural" type (essentially the most efficient).

    Also look at stdint.h, which defines types such as int_fast16_t, which is the "fastest" type with at least 16 bits.  For C6000, this is signed int.