Title: Performance gap of match statements of classes compared with equivalent Python 3.9 code · Issue #92868 · python/cpython · GitHub
Open Graph Title: Performance gap of match statements of classes compared with equivalent Python 3.9 code · Issue #92868 · python/cpython
X Title: Performance gap of match statements of classes compared with equivalent Python 3.9 code · Issue #92868 · python/cpython
Description: 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...
Open Graph Description: 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 _...
X Description: 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 _...
Opengraph URL: https://github.com/python/cpython/issues/92868
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Performance gap of match statements of classes compared with equivalent Python 3.9 code","articleBody":"Im doing some micro-benchmarks of the `match` statement.\r\n\r\nThe following comparison shows two functionally equivalent functions of the class matching pattern in the `match` statement.\r\n\r\n```python\r\nclass Driver:\r\n def __init__(self, name, team, **extra):\r\n self.name = name\r\n self.team = team\r\n self.extra = extra\r\n\r\ndef bench_class_matching_statement():\r\n drivers = [\r\n Driver(name=\"Max Verstappen\", team=\"Red Bull\", ),\r\n Driver(name=\"Sergio Perez\", team=\"Red Bull\", ),\r\n Driver(name=\"Charles Leclerc\", team=\"Ferrari\", ),\r\n Driver(name=\"Lewis Hamilton\", team=\"Mercedes\", ),\r\n ]\r\n\r\n for _ in range(100_000):\r\n for driver in drivers:\r\n match driver:\r\n case Driver(name=\"Max Verstappen\"): desc = f\"Max Verstappen, the current world #1\"\r\n case Driver(name=name, team=\"Ferrari\"): desc = f\"{name}, a Ferrari driver!! 🐎\"\r\n case Driver(name=name, team=team): desc = f\"{name}, a {team} driver.\"\r\n case _: desc = \"Invalid request\"\r\n # print(desc)\r\n\r\n\r\ndef bench_class_matching_logical():\r\n drivers = [\r\n Driver(name=\"Max Verstappen\", team=\"Red Bull\", ),\r\n Driver(name=\"Sergio Perez\", team=\"Red Bull\", ),\r\n Driver(name=\"Charles Leclerc\", team=\"Ferrari\", ),\r\n Driver(name=\"Lewis Hamilton\", team=\"Mercedes\", ),\r\n ]\r\n\r\n for _ in range(100_000):\r\n for driver in drivers:\r\n if not isinstance(driver, Driver):\r\n desc = \"Invalid request\"\r\n elif driver.name == \"Max Verstappen\":\r\n desc = f\"Max Verstappen, the current world #1\"\r\n elif driver.team == \"Ferrari\": \r\n desc = f\"{driver.name}, a Ferrari driver!! 🐎\"\r\n else:\r\n desc = f\"{driver.name}, a {driver.team} driver.\"\r\n # print(desc)\r\n```\r\n\r\nPython 3.11 executes `bench_class_matching_statement()` at 4x the execution time of `bench_class_matching_logical()`\r\nPython 3.10 executes `bench_class_matching_statement()` at 2.5x the execution time of `bench_class_matching_logical()`\r\n\r\nPython 3.11b1 is showing a speedup of both functions (which is great). \r\n\r\nPattern matching for sequences and mapping is faster than the equivalent Python code, but for classes it is significantly slower.","author":{"url":"https://github.com/tonybaloney","@type":"Person","name":"tonybaloney"},"datePublished":"2022-05-17T04:29:47.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":4},"url":"https://github.com/92868/cpython/issues/92868"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:25896fea-4247-4a07-bbef-1e73c3fd4a13 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B35A:2818E8:11F914:18AC95:6A525268 |
| html-safe-nonce | c5c7729439b7e424362960c7235c3c778d47a53868beec41353365be6765fb10 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMzVBOjI4MThFODoxMUY5MTQ6MThBQzk1OjZBNTI1MjY4IiwidmlzaXRvcl9pZCI6IjUyNDI4OTMzNjc3MjIwMDUwOTYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 697738d163f185deeef6c4df9cbac4d13abeef64790df94849f969c2bfc0261d |
| hovercard-subject-tag | issue:1238048738 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/92868/issue_layout |
| twitter:image | https://opengraph.githubassets.com/bcd1c324e025b754d384ddfd910a1a2329ca1928d05bd482ceb3bf265bc0027e/python/cpython/issues/92868 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/bcd1c324e025b754d384ddfd910a1a2329ca1928d05bd482ceb3bf265bc0027e/python/cpython/issues/92868 |
| og:image:alt | 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 _... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | tonybaloney |
| hostname | github.com |
| expected-hostname | github.com |
| None | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 7aed05249554b889eb33d002851a973eebcc7e91 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width