r/cpp • • Feb 16 '26

Favorite optimizations ??

I'd love to hear stories about people's best feats of optimization, or something small you are able to use often!

135 Upvotes

185 comments sorted by

View all comments

Show parent comment

1

u/Successful_Yam_9023 Feb 17 '26

For the integer modulo, another thing it can sometimes be replaced with (depending on context) is a multiplication and shift-right (the shift may compile to zero instructions), like this: https://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/