Operating System — How to Write CPU-Efficient Code

Tony
8 min readDec 20, 2023

The CPU is the engine that runs code, and the effectiveness of our coding can significantly influence the CPU’s performance. This is particularly crucial when crafting computation-heavy applications, as poor attention to CPU efficiency can drastically degrade overall system performance.

Embedded within the CPU is the CPU Cache, a small but swiftly accessible memory located very near to the CPU core. Access speeds of this cache are incredibly fast, so computations can be expedited when data is fetched from the cache rather than main memory.

Yet, many developers are not familiar with the intricacies of CPU Cache operation, which can lead to missed opportunities for writing code that optimizes cache usage. Mastering this can open up new avenues for code optimization.

Why CPU Cache

You might be curious about the necessity of CPU Cache when we have RAM. Due to Moore’s Law, CPU speeds double roughly every 18 months, about a 60% increase per year. Memory speeds also improve, but much slower than CPUs — by only about 7% per year. This creates a growing disparity in performance between CPUs and memory.

Now, a memory access can require 200 to 300 clock cycles, indicating the CPU and memory speed differ by 200 to 300 times. To mitigate this discrepancy, CPU…

--

--