You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Performance gap of match statements of classes compared with equivalent Python 3.9 code #92868
Im doing some micro-benchmarks of the match statement.
The following comparison shows two functionally equivalent functions of the class matching pattern in the match statement.
classDriver:
def__init__(self, name, team, **extra):
self.name=nameself.team=teamself.extra=extradefbench_class_matching_statement():
drivers= [
Driver(name="Max Verstappen", team="Red Bull", ),
Driver(name="Sergio Perez", team="Red Bull", ),
Driver(name="Charles Leclerc", team="Ferrari", ),
Driver(name="Lewis Hamilton", team="Mercedes", ),
]
for_inrange(100_000):
fordriverindrivers:
matchdriver:
caseDriver(name="Max Verstappen"): desc=f"Max Verstappen, the current world #1"caseDriver(name=name, team="Ferrari"): desc=f"{name}, a Ferrari driver!! 🐎"caseDriver(name=name, team=team): desc=f"{name}, a {team} driver."case _: desc="Invalid request"# print(desc)defbench_class_matching_logical():
drivers= [
Driver(name="Max Verstappen", team="Red Bull", ),
Driver(name="Sergio Perez", team="Red Bull", ),
Driver(name="Charles Leclerc", team="Ferrari", ),
Driver(name="Lewis Hamilton", team="Mercedes", ),
]
for_inrange(100_000):
fordriverindrivers:
ifnotisinstance(driver, Driver):
desc="Invalid request"elifdriver.name=="Max Verstappen":
desc=f"Max Verstappen, the current world #1"elifdriver.team=="Ferrari":
desc=f"{driver.name}, a Ferrari driver!! 🐎"else:
desc=f"{driver.name}, a {driver.team} driver."# print(desc)
Python 3.11 executes bench_class_matching_statement() at 4x the execution time of bench_class_matching_logical()
Python 3.10 executes bench_class_matching_statement() at 2.5x the execution time of bench_class_matching_logical()
Python 3.11b1 is showing a speedup of both functions (which is great).
Pattern matching for sequences and mapping is faster than the equivalent Python code, but for classes it is significantly slower.
So just to be clear, both functions are faster in 3.11 than in 3.10, but the bug is that the speedup in one of them is greater than in the other? That hardly seems like a bug.
The bug is that pattern matching of classes per the spec is slower than the equivalent Python 3.9< code.
I had a conversation with Brandt about this the other week. He was working on a patch to improve the performance, so I wanted to capture this issue to track it.
FF to close if this should go somewhere else (mailing lists)
changed the title [-]Python 3.11 performance regression in match statements of classes[/-][+]Performance gap of match statements of classes compared with equivalent Python 3.9 code[/+]on May 17, 2022
Activity
tonybaloney commentedon May 17, 2022
CC @markshannon @brandtbucher
JelleZijlstra commentedon May 17, 2022
So just to be clear, both functions are faster in 3.11 than in 3.10, but the bug is that the speedup in one of them is greater than in the other? That hardly seems like a bug.
tonybaloney commentedon May 17, 2022
Sorry, that was unclear.
The bug is that pattern matching of classes per the spec is slower than the equivalent Python 3.9< code.
I had a conversation with Brandt about this the other week. He was working on a patch to improve the performance, so I wanted to capture this issue to track it.
FF to close if this should go somewhere else (mailing lists)
[-]Python 3.11 performance regression in match statements of classes[/-][+]Performance gap of match statements of classes compared with equivalent Python 3.9 code[/+]eendebakpt commentedon Apr 28, 2026
#138915 improved the performance of
match. This can probably be closed as a duplicate of (the newer) #138912