Tuesday, April 4, 2017

Effect of variable typecasting, on the underneath assembly code size


  1. Whenever we type-cast we are actually adding or no-effect on the number of instructions added to code.
  2. If lets say, the controller is 16-bit, and we have uint32_t a = 5, b; b = a *10;
  3. In this case b = a*10 will be performed in 2 parts multiplication. Hence a single multiplication will be replaced by two multiplications
  4. If we typecast it like, b = (uint16_t)a * 10; then it will be replaced by code which will be a single cycle multiplication operation.
  5. Int to float or vice-verse takes lot of time, as the numbers are representations are different and there will instructions of number conversion getting added. This will increase the processing time a lot, even though you see it as a simple one line multiplication in C code.
  6. Hence, always be careful when declaring float in the code. If you dont need that kind of precision, dont use it. Use Fixed point representation instead.
  7. Probably down casting a variable usually reduces the underneath instructions
  8. Processor lets say of 32bit will be optimally used if the data variables to be processed are of size 32bit

2 comments:

  1. I take from this, that type casting should be avoided as much as possible at least in competitive coding, since it will increase execution time.

    ReplyDelete
    Replies
    1. well usually when you are playing in integer i.e. 8bit, 16bit, 32bit, 64bit its ok, but when you change from integer to float, it takes time. You may not see it on code which runs on PC kind of machine. but if your code has many number of conversions and its running on some webserver backend then it will affect the performance :)

      Delete

PROFILE

My photo
India
Design Engineer ( IFM Engineering Private Limited )

Followers