Skip to content

Optimize memoryview comparison: a memoryview is equal to itself #144995

@vstinner

Description

@vstinner

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usagetype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions