I was wondering: on the x86 architecture, does it take more time/CPU cycles to read a value from a register, or to read a value from RAM? I would assume RAM, since the CPU has to interface with an address/memory bus of some sort, whereas with a register it reads from a hardcoded area. Is this correct, or is there some other factor to take into account?
Asked
Active
Viewed 324 times
0
-
Look at compiler-generated asm. It always uses registers when it can, not memory. If the answer to this question wasn't obvious to you, go read https://agner.org/optimize/ and other performance links in https://stackoverflow.com/tags/x86/info. – Peter Cordes Jun 09 '20 at 04:23
-
Related: [Why are CPU registers fast to access?](https://stackoverflow.com/q/3518445) - x86 is no exception to the rule that registers are even faster than L1d cache. – Peter Cordes Jun 09 '20 at 04:58
1 Answers
2
Registers exist on the CPU, memory must be read from a chip over a bus. So the register should always be faster.
user2199860
- 788
- 4
- 14
-
3L1d cache also exists inside each CPU core, but yes reading registers is even faster (lower latency, fewer uops), not having to go through a load port. Note this is tagged x86, and all x86 CPUs have had caches for over 25 years. (Not counting 8086-based microcontrollers.) – Peter Cordes Jun 09 '20 at 04:19
-
1However, reading some special registers (like control registers, debug registers, or MSRs) can be slower. – Andreas Abel Jun 09 '20 at 17:24