Is Python Code Sensitive to CPU Caching? (2024)
lukasatkinson.de
Is Python Code Sensitive to CPU Caching? (2024)
1–10 of 39 posts
Re: Is Python Code Sensitive to CPU Caching? (2024)
#2Re: Is Python Code Sensitive to CPU Caching? (2024)
#3IME 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.
I would be happy to stand corrected if someone knows more about this, though!
Re: Is Python Code Sensitive to CPU Caching? (2024)
#4IME 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.
It should be, as most interpreted code uses a JIT native compiler at this point, which implies the branch prediction is native as well. I would be happy to stand corrected if someone knows more about this, though!
Re: Is Python Code Sensitive to CPU Caching? (2024)
#5IME 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.
It should be, as most interpreted code uses a JIT native compiler at this point, which implies the branch prediction is native as well. I would be happy to stand corrected if someone knows more about this, though!
I suspect most JavaScript code is JIT-compiled - but most Python, Ruby etc is interpreted (via bytecode).
Re: Is Python Code Sensitive to CPU Caching? (2024)
#6Earlier quoted context omitted.
It should be, as most interpreted code uses a JIT native compiler at this point, which implies the branch prediction is native as well. I would be happy to stand corrected if someone knows more about this, though!
AFAIK CPython doesn't JIT compile.
Re: Is Python Code Sensitive to CPU Caching? (2024)
#7Earlier quoted context omitted.
AFAIK CPython doesn't JIT compile.
Recent versions of CPython do have an experimental JIT compiler. I'm not sure how widely-used it is, though. https://peps.python.org/pep-0744/
Re: Is Python Code Sensitive to CPU Caching? (2024)
#8Re: Is Python Code Sensitive to CPU Caching? (2024)
#9IME 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.
Re: Is Python Code Sensitive to CPU Caching? (2024)
#10IME 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.
It's the classic, why is processing sorted array faster than unsorted one
def f(arr):
r = True
for x in arr:
if x > 128: r = not r
return r
arr = [ random.randint(0, 255) for i in range(0, 1000_000) ]
arr_sorted = list(sorted(arr))
%timeit f(arr)
%timeit f(arr_sorted)
Results are (on my machine): 17.5 ms for unsorted, and 13.5 ms for sorted. For comparison, in a compiled language, the difference is 4x