TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Is Python Code Sensitive to CPU Caching? (2024)

81 pointsby leonryabout 2 months ago

4 comments

PhilipRomanabout 2 months ago
IME CPU caches can be observed in almost all languages, regardless of how detached they are from the machine. What I'm really wondering is, if branch prediction can be observed from interpreted code.
评论 #43593810 未加载
评论 #43594317 未加载
评论 #43593809 未加载
评论 #43593468 未加载
评论 #43596812 未加载
PaulHouleabout 2 months ago
"Mechanical sympathy" effects are so strong on modern CPUs you feel them even in languages like Python that are far from the metal.
Delkabout 2 months ago
I really don&#x27;t see why you wouldn&#x27;t expect to find cache-sensitivity in Python, or in some other similarly high-level language.<p>Python sequences are defined as &quot;support[ing] efficient element access using integer indices&quot; [1]. Python lists are sequences and thus must support random access. In practice that means the implementation is a (dynamically resizing) array allocated contiguously. That means spatial locality is relevant.<p>If the list type were defined simply as an iterable collection, with no requirement of constant-time random access, the definition would be abstract enough that an implementation might end up being something else than a contiguous array. But if you define the type so that it supports constant-time random access [2], you pretty much end up with cache-friendly sequential access as well.<p>If you don&#x27;t define the list type as supporting random access, you also sacrifice asymptotic algorithmic efficiency for lookups by index. Any language that cares about efficiency <i>at all</i> separates collections that support efficient random access from those that don&#x27;t. (For example, Python has lists and dicts&#x2F;sets for different access patterns. The Java standard library has separate types for contiguous arrays&#x2F;lists, hashtable-backed collections and tree-backed collections because the three have different algorithmic efficiencies for different access patterns. In practice it leads to different properties in terms of cache-friendliness as well.)<p>[1] <a href="https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;glossary.html#term-sequence" rel="nofollow">https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;glossary.html#term-sequence</a><p>[2] As usual, the Python documentation is a bit vague on the details. It doesn&#x27;t really say random access has to be constant-time, only that it has to be &quot;efficient&quot;. So you might be able to have a non-constant time implementation such as an indexable skiplist while arguing that it&#x27;s efficient, but you&#x27;d have to go out of your way to do that.
评论 #43597306 未加载
davvidabout 2 months ago
The article didn&#x27;t mention Python&#x27;s atomic refcounts. They&#x27;re very bad for cache usage as they&#x27;re constantly invalidating cache.
评论 #43595613 未加载
评论 #43596486 未加载