-
-
Notifications
You must be signed in to change notification settings - Fork 34.1k
Open
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usagePerformance or resource usagetype-featureA feature request or enhancementA feature request or enhancement
Description
Comparing a memoryview to itself compares all values which is slow (O(n) complexity). Comparing a bytes string to itself returns True without comparing each byte.
from timeit import timeit
with open("/dev/random", 'br') as fp:
data = fp.read(2**20)
view = memoryview(data)
LOOPS = 1_000
b = timeit('x == x', number=LOOPS, globals={'x': data})
m = timeit('x == x', number=LOOPS, globals={'x': view})
print("bytes %f seconds" % b)
print("mview %f seconds" % m)
print("⇒ %f time slower" % (m / b))Output on Python 3.14:
bytes 0.000027 seconds
mview 2.663470 seconds
⇒ 97806.240268 time slower
I propose to implement the same optimization than bytes in memoryview: return True immediately when comparing a memoryview to itself.
We should just pay attention to float types which cannot use the optimization. Floats can be equal to NaN (Not-a-Number) and NaN is not equal to itself.
Linked PRs
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usagePerformance or resource usagetype-featureA feature request or enhancementA feature request or enhancement