Hey!
Consider the following solutions for the CSES problem Coin Combination I (Time Limit is 1s):
Note that using const
or constexpr
in the first solution is the same since we are initializing a global variable with a prvalue literal.
The first solution passes with Runtime ~0.58s in the worst test case, whereas the second solution gives a TLE outcome.
It seems that reading from compile time constants may be twice as fast as reading from arbitrary variables. Is there a particular reason for this?
EDIT: Big thanks to AkibAzmain and MattTheNub, I got the following answers:
- Compile time constants are inlined by the compiler which saves multiple machine register reading operations.
- When $$$mod$$$ is constant the modulo operator uses a technique called Barret Reduction to speed up computations.