Search code examples
operation

Which one is better performance wise?


Performance wise, which is better:

a. filesizeInMB = filesize / (1024 * 1024)

or

b. filesizeInMB = filesize / 1024 / 1024


Solution

  • It is often better to avoid division. Thus a would be better.

    On the other hand. If your compiler is smart. It could maybe see that dividing by 1024 is equal to a bit shifting operation. In that case b could be even faster.