Closed ghost closed 3 years ago
What you read is right, and it's not restricted to C++. CPUs are significantly slower at division than multiplication. However, the issue only applies when you're dividing by a variable.
Whenever you're dividing by a constant that's known at compile time, the compiler will just convert it to a multiplication at optimization time, similar to what you're doing by hand here.
Furthermore, some of the cases you're highlighting are divisions by an integer; you can't replace them by a multiplication with a floating point number (the result will be a different data type). It is possible to convert integer division by a constant to something faster as well, but it's a little more complicated (depending on the situation, it may involve multiplications, shifts, or a combination of the two).
Lastly, none of the cases you point out are performance critical. It's better to focus optimization efforts on places where it matters.
Thanks @sipa that answers my question. Will close the issue.
I was reading about multiplication being faster than division in C++: https://stackoverflow.com/a/17883577/
99% of the regular contributors in this repository know C++ better than me so I think this must have already been discussed or considered while doing calculations although I couldn't find anything in doc/developer-notes.md
Division is done at lot of places which could be replaced with multiplication. Are there any downsides?
Example:
GetMinFee()
in src/txmempool.cpp