When writing integer functions in Rust which will run millions of times (think pixel processing), it's useful to use operations with the highest performance - similar to C/C++.
While the reference manual explains changes in behavior, it's not always clear which methods are higher performance than the standard (see note 1.) integer arithmetic operations. I'd assume wrapping_add
compiles down to something equivalent to C's addition.
Of the standard operations (add / subtract / multiply / modulo / divide / shift / bit manipulation...), which operations have higher performance alternatives which aren't used by default?
Note:
- By standard I mean integer arithmetic using symbols
a + b
,i / k
orc % e
... etc.
What you would use when writing math expressions - unless you have a special need for using one of the methods that wraps or returns the overflow. - I realize answering this question may require some research. So I'm happy to do some checks by looking at resulting assembly to see which operations are using unchecked/primitive operations.
- It may be that the speed difference between checked / unchecked operations isn't significant, if that's the case I'd still like to be able to write a 'fast' version of a function to compare against the 'safe' version, to come to my own conclusion as to whether it's a reasonable choice for a given function.
- Having mentioned pixel-processing, SIMD has come up as a possible solution. Even though this is a good suggestion. That still leaves us with the cases which can't be optimized using SIMD, so the general case of fast integer arithmetic is still something to consider.