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.
The text was updated successfully, but these errors were encountered:
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)
tonybaloney
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
May 17, 2022
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.Python 3.11 executes
bench_class_matching_statement()
at 4x the execution time ofbench_class_matching_logical()
Python 3.10 executes
bench_class_matching_statement()
at 2.5x the execution time ofbench_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.
The text was updated successfully, but these errors were encountered: