Get the latest tech news

Is Python Code Sensitive to CPU Caching? (2024)


-aware programming can make a huge performance difference, especially when writing code in C++ or Rust. Python is a much more high-level language, and doesn't give us that level of control over memory layout of our data structures.

I don't want to bother with strings and number parsing, so let's use Python's struct and array modules to pack the test data into a binary representation. Part of this is due to the lower amount of iterations, so that the initialization overhead (Python interpreter boot, parsing the input data) becomes more noticeable. That the pure-Python version suffered a greater slowdown could indicate that the more complex PyObject* layout is substantially more cache-hostile, but I have a hunch that this might not be the entire story, e.g. because these measurements also include input parsing overhead.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of Python code

Python code

Related news:

News photo

Python 3.14 Lands A New Interpreter With 3~30% Faster Python Code

News photo

Building a full-text search engine in 150 lines of Python code (2021)

News photo

How to debug Python code in Visual Studio Code