Skip to content

Performance gap of match statements of classes compared with equivalent Python 3.9 code #92868

Closed

Description

@tonybaloney

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.

class Driver:
    def __init__(self, name, team, **extra):
        self.name = name
        self.team = team
        self.extra = extra

def bench_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 _ in range(100_000):
        for driver in drivers:
            match driver:
                case Driver(name="Max Verstappen"): desc = f"Max Verstappen, the current world #1"
                case Driver(name=name, team="Ferrari"): desc = f"{name}, a Ferrari driver!! 🐎"
                case Driver(name=name, team=team): desc = f"{name}, a {team} driver."
                case _: desc = "Invalid request"
            # print(desc)


def bench_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 _ in range(100_000):
        for driver in drivers:
            if not isinstance(driver, Driver):
                desc = "Invalid request"
            elif driver.name == "Max Verstappen":
                desc = f"Max Verstappen, the current world #1"
            elif driver.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.

Activity

  1. tonybaloney commented on May 17, 2022

    @tonybaloney
    ContributorAuthor
  2. JelleZijlstra commented on May 17, 2022

    @JelleZijlstra
    Member

    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.

  3. tonybaloney commented on May 17, 2022

    @tonybaloney
    ContributorAuthor

    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)

  4. 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
  5. self-assigned this
    on May 17, 2022
  6. eendebakpt commented on Apr 28, 2026

    @eendebakpt
    Contributor

    #138915 improved the performance of match. This can probably be closed as a duplicate of (the newer) #138912

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

3.12only security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usagetype-bugAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions