{"id":394637,"date":"2024-06-29T11:44:59","date_gmt":"2024-06-29T11:44:59","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=394637"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=394637","title":{"rendered":"<span>Match\/case vs If\/else. \u0421\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u043d\u0438\u0435\u043c \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u044b \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u0432 \u0432 Python 3.10<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u0440\u043e\u0448\u043b\u043e \u0443\u0436\u0435 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0441 \u043c\u043e\u043c\u0435\u043d\u0442\u0430 \u0440\u0435\u043b\u0438\u0437\u0430 Python \u0432\u0435\u0440\u0441\u0438\u0438 3.10. \u0421\u0430\u043c\u044b\u043c \u0433\u043b\u0430\u0432\u043d\u044b\u043c \u0438 \u0441\u0430\u043c\u044b\u043c \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u043c \u0431\u044b\u043b\u043e \u0432\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430 match\/case (\u043e\u043d \u0436\u0435 pattern matching).\u00a0<\/p>\n<p>\u041e\u0434\u043d\u0430\u043a\u043e \u0434\u0430\u043b\u0435\u043a\u043e \u043d\u0435 \u0432\u0441\u0435\u043c \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430\u043c \u0438\u0437 \u043d\u0430\u0448\u0435\u0433\u043e \u043a\u043e\u043c\u044c\u044e\u043d\u0438\u0442\u0438 \u0437\u0430\u0448\u0435\u043b \u0434\u0430\u043d\u043d\u044b\u0439 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440. \u0421\u0432\u0438\u0434\u0435\u0442\u0435\u043b\u044c\u0441\u0442\u0432\u0443\u044e\u0442 \u044d\u0442\u043e\u043c\u0443 \u0434\u0430\u0436\u0435 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438 \u043f\u043e\u0434 \u0441\u0442\u0430\u0442\u044c\u044f\u043c\u0438 \u043d\u0430 \u0445\u0430\u0431\u0440\u0435 (<a href=\"https:\/\/habr.com\/ru\/post\/585216\/\" rel=\"noopener noreferrer nofollow\">\u0441\u0442\u0430\u0442\u044c\u044f 1<\/a>,\u00a0<a href=\"https:\/\/habr.com\/ru\/post\/585518\/\" rel=\"noopener noreferrer nofollow\">\u0441\u0442\u0430\u0442\u044c\u044f 2<\/a>), \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u044b\u043b\u0438 \u043f\u043e\u0441\u0432\u044f\u0449\u0435\u043d\u044b match\/case.<\/p>\n<p>\u041d\u0430 \u043c\u043e\u0439 \u0432\u0437\u0433\u043b\u044f\u0434, \u043d\u043e\u0432\u044b\u0439 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 \u0443\u043f\u0440\u043e\u0449\u0430\u0435\u0442 \u0436\u0438\u0437\u043d\u044c \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430\u043c, \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u044f \u043d\u0430 \u0441\u0435\u0431\u044f \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u043e\u0439 \u0442\u0438\u043f\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438\u043b\u0438 \u043f\u0440\u0438\u043d\u0430\u0434\u043b\u0435\u0436\u043d\u043e\u0441\u0442\u044c\u044e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u043c\u0443 \u043a\u043b\u0430\u0441\u0441\u0443. \u041d\u043e, \u043a\u0430\u043a \u043c\u044b \u0432\u0441\u0435 \u0437\u043d\u0430\u0435\u043c, \u0437\u0430\u0447\u0430\u0441\u0442\u0443\u044e \u0437\u0430 \u043a\u0440\u0443\u0442\u044b\u0435 \u0444\u0438\u0447\u0438, \u0432\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0435 \u0432 \u044f\u0437\u044b\u043a, \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442\u0443 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043f\u043b\u0430\u0442\u0438\u0442\u044c. \u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u0445\u043e\u0442\u0435\u043b \u0431\u044b \u043e\u0441\u0432\u0435\u0442\u0438\u0442\u044c \u0442\u0435\u043c\u0443 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430 match\/case \u0438 \u0441\u0440\u0430\u0432\u043d\u0438\u0442\u044c \u0435\u0433\u043e \u0441 \u043e\u0431\u044b\u0447\u043d\u044b\u043c if\/else.<\/p>\n<h3>\u041d\u0430\u0447\u0438\u043d\u0430\u0435\u043c<\/h3>\n<p>\u0421\u0430\u043c\u044b\u0439 \u043e\u0431\u044b\u0434\u0435\u043d\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440, \u043a\u043e\u0433\u0434\u0430 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043f\u043b\u043e\u0434\u0438\u0442\u044c \u043f\u0435\u0440\u0435\u0431\u043e\u0440\u044b if\/else &#8212; \u044d\u0442\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u0440\u0430\u0432\u043d\u0438\u0442\u044c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0441 \u043a\u0430\u043a\u0438\u043c\u0438-\u043b\u0438\u0431\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438. \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u043b\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0445 \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445:<\/p>\n<pre><code class=\"python\">import random as rnd def create_rnd_data():     words = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0,          '\u0437\u0434\u0435\u0441\u044c', '\u0434\u043e\u043c', '\u0434\u0430', '\u043f\u043e\u0442\u043e\u043c\u0443', '\u0441\u0442\u043e\u0440\u043e\u043d\u0430',          '\u043a\u0430\u043a\u043e\u0439-\u0442\u043e', '\u0434\u0443\u043c\u0430\u0442\u044c', '\u0441\u0434\u0435\u043b\u0430\u0442\u044c', '\u0441\u0442\u0440\u0430\u043d\u0430',          '\u0436\u0438\u0442\u044c', '\u0447\u0435\u043c', '\u043c\u0438\u0440', '\u043e\u0431', '\u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439', '\u0441\u043b\u0443\u0447\u0430\u0439',          '\u0433\u043e\u043b\u043e\u0432\u0430', '\u0431\u043e\u043b\u0435\u0435', '\u0434\u0435\u043b\u0430\u0442\u044c', '\u0447\u0442\u043e-\u0442\u043e', '\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c',          '\u0440\u0435\u0431\u0435\u043d\u043e\u043a', '\u043f\u0440\u043e\u0441\u0442\u043e', '\u043a\u043e\u043d\u0435\u0447\u043d\u043e', '\u0441\u0438\u043b\u0430', '\u0440\u043e\u0441\u0441\u0438\u0439\u0441\u043a\u0438\u0439',          '\u043a\u043e\u043d\u0435\u0446', '\u043f\u0435\u0440\u0435\u0434', '\u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e']     data = rnd.choices(words, k=500000)     return data<\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u043e\u0441\u0442\u044b\u0445 \u043f\u0440\u043e\u0432\u0435\u0440\u043e\u043a \u0434\u0430\u043d\u043d\u044b\u0445, \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c\u044b\u0445 \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438\u00a0<code>create_rnd_data:<\/code><\/p>\n<pre><code class=\"python\"># \u043f\u0440\u043e\u0441\u0442\u044b\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 if\/else def test_if(data):     for word in data:         if word in ['\u0434\u043e\u043c', '\u0434\u0443\u043c\u0430\u0442\u044c', '\u0447\u0442\u043e-\u0442\u043e', '\u043f\u0440\u043e\u0441\u0442\u043e']:             pass         elif isinstance(word, int):             pass         elif isinstance(word, str) and len(word) > 3:             pass         elif isinstance(word, str) and word.startswith(\"\u0434\"):             pass         else:             pass # \u0442\u0435 \u0436\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 match\/case def test_match(data):     for word in data:         match word:             case '\u0434\u043e\u043c'|'\u0434\u0443\u043c\u0430\u0442\u044c'|'\u0447\u0442\u043e-\u0442\u043e'|'\u043f\u0440\u043e\u0441\u0442\u043e':                 pass             case int(word):                 pass             case str(word) if len(word) > 3:                 pass             case str(word) if word.startswith(\"\u0434\"):                 pass             case _:                 pass<\/code><\/pre>\n<p>\u0421\u0430\u043c\u043e\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0435: \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 \u043c\u043e\u0434\u0443\u043b\u044f timeit \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c \u0432\u0440\u0435\u043c\u044f, \u0437\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0432 \u0441\u0440\u0435\u0434\u043d\u0435\u043c \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f \u043a\u0430\u0436\u0434\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f. \u041f\u0440\u043e\u0432\u0435\u0434\u0451\u043c 1000 \u0438\u0441\u043f\u044b\u0442\u0430\u043d\u0438\u0439 \u043a\u0430\u0436\u0434\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438:<\/p>\n<pre><code class=\"python\">import timeit # \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0434\u043b\u044f \u0442\u0435\u0441\u0442\u0430 test_data = create_rnd_data() # \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u043e\u0432\u0442\u043e\u0440\u0435\u043d\u0438\u0439 repeats = 1000 # \u0441\u0447\u0438\u0442\u0430\u0435\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b time_repeat_if = timeit.timeit(\"test_if(test_data)\",                                setup=\"from __main__ import test_if, test_data\",                                number=repeats)  time_repeat_match = timeit.timeit(\"test_match(test_data)\",                                   setup=\"from __main__ import test_match, test_data\",                                   number=repeats) print(\"\u0420\u0415\u0417\u0423\u041b\u042c\u0422\u0410\u0422 IF\/ELSE:   \", time_repeat_if\/repeats) print(\"\u0420\u0415\u0417\u0423\u041b\u042c\u0422\u0410\u0422 MATCH\/CASE:\", time_repeat_match\/repeats) >>> \u0420\u0415\u0417\u0423\u041b\u042c\u0422\u0410\u0422 IF\/ELSE:    0.1284590820000085 >>> \u0420\u0415\u0417\u0423\u041b\u042c\u0422\u0410\u0422 MATCH\/CASE: 0.4847222329999931<\/code><\/pre>\n<details class=\"spoiler\">\n<summary>\u0417\u0430\u0447\u0435\u043c \u0434\u0435\u043b\u0430\u044e\u0442\u0441\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 isinstance \u0432 if\/else?<\/summary>\n<div class=\"spoiler__content\">\n<p>\u0422\u0430\u043a \u043a\u0430\u043a \u0432 match\/case \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0442\u0438\u043f\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 int, str, \u0447\u0442\u043e\u0431\u044b \u0432\u0441\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0432 if\/else \u0431\u044b\u043b\u0438 \u044d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430\u043c match\/case, \u0442\u043e \u0432\u0437\u0430\u043c\u0435\u043d str, int \u0434\u0435\u043b\u0430\u044e\u0442\u0441\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 isinstance(&#8230;, str\/int). \u0422\u0430\u043a \u0436\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c, \u0447\u0442\u043e Python &#8212; \u044f\u0437\u044b\u043a \u0441 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0442\u0438\u043f\u0438\u0437\u0430\u0446\u0438\u0435\u0439, \u0438 \u043d\u0435 \u0432\u0441\u0435\u0433\u0434\u0430 \u0435\u0441\u0442\u044c \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u0441\u043f\u0438\u0441\u043a\u0435 \u0431\u0443\u0434\u0443\u0442 \u0447\u0435\u0442\u043a\u043e \u0442\u043e\u0433\u043e \u0442\u0438\u043f\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b \u043e\u0436\u0438\u0434\u0430\u0435\u0442\u0435.<\/p>\n<\/div>\n<\/details>\n<p>\u0425\u043c\u043c\u043c\u2026 \u0412 3.8 \u0440\u0430\u0437\u0430 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f match\/case \u043e\u043a\u0430\u0437\u0430\u043b\u0430\u0441\u044c \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u0435\u0435, \u0447\u0435\u043c \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c if\/else. \u041d\u043e \u043d\u0435 \u0431\u0443\u0434\u0435\u043c \u0437\u0430\u0433\u0430\u0434\u044b\u0432\u0430\u0442\u044c \u043d\u0430\u043f\u0435\u0440\u0435\u0434, \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c, match\/case \u043e\u043a\u0430\u0436\u0435\u0442\u0441\u044f \u0431\u044b\u0441\u0442\u0440\u0435\u0435 \u043f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u044b\u043c\u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430\u043c\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u0440\u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0435 \u0441\u043b\u043e\u0432\u0430\u0440\u0435\u0439.<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043b\u043e\u0432\u0430\u0440\u0435\u0439:<\/p>\n<pre><code class=\"python\">def create_rnd_data():     names = [\"phone\", \"TV\", \"PC\", \"car\", \"home\", \"case\", \"bird\", \"chicken\",             \"dish\", \"float\", \"C++\", \"data\", \"\"]     prices = [500, 100, 1400, 2000, 750, 3500, 5000, 120, 50, 4200]     goods = []     for i in range(500000):         name = names[i%len(names)]         price = prices[i%len(prices)]         goods.append({\"name\": name, \"price\": price})     return goods<\/code><\/pre>\n<p>\u041f\u0440\u043e\u0432\u0435\u0440\u0438\u043c, \u043a\u0430\u043a \u043f\u043e\u0432\u0435\u0434\u0443\u0442 \u0441\u0435\u0431\u044f if\/else \u0438 match\/case \u043f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441\u043e \u0441\u043b\u043e\u0432\u0430\u0440\u044f\u043c\u0438:<\/p>\n<pre><code class=\"python\">import timeit def test_if(data):     for element in data:         if element.get(\"name\") in [\"phone\", \"TV\"] and isinstance(element.get(\"price\"), int) and element.get(\"price\") > 2000:             pass         elif element.get(\"name\") == \"case\" and isinstance(element.get(\"price\"), int) and element.get(\"price\") &lt;= 750:             pass         elif element.get(\"name\") == \"case\" and isinstance(element.get(\"price\"), int) and element.get(\"price\") == 750:             pass         elif isinstance(element.get(\"name\"), str) and element.get(\"name\"):             pass         elif isinstance(element.get(\"price\"), int) and element.get(\"price\") > 1000:             pass         else:             pass def test_match(data):     for element in data:         match element:             case {\"name\": \"phone\"|\"TV\", \"price\": int(price)} if price > 2000:                 pass             case {\"name\": \"case\", \"price\": int(price)} if price &lt;= 750:                 pass             case {\"name\": \"case\", \"price\": 750}:                 pass             case {\"name\": str(name), \"price\": _} if name:                 pass             case {\"name\": _, \"price\": int(price)} if price > 1000:                 pass             case _:                 pass  # \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0434\u043b\u044f \u0442\u0435\u0441\u0442\u0430 test_data = create_rnd_data() # \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u043e\u0432\u0442\u043e\u0440\u0435\u043d\u0438\u0439 repeats = 1000 # \u0441\u0447\u0438\u0442\u0430\u0435\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b time_repeat_if = timeit.timeit(\"test_if(test_data)\",                                setup=\"from __main__ import test_if, test_data\",                                number=repeats)  time_repeat_match = timeit.timeit(\"test_match(test_data)\",                                   setup=\"from __main__ import test_match, test_data\",                                   number=repeats) print(\"\u0420\u0415\u0417\u0423\u041b\u042c\u0422\u0410\u0422 IF\/ELSE:   \", time_repeat_if\/repeats) print(\"\u0420\u0415\u0417\u0423\u041b\u042c\u0422\u0410\u0422 MATCH\/CASE:\", time_repeat_match\/repeats) >>> \u0420\u0415\u0417\u0423\u041b\u042c\u0422\u0410\u0422 IF\/ELSE:   0.25263675300000616 >>>\u0420\u0415\u0417\u0423\u041b\u042c\u0422\u0410\u0422 MATCH\/CASE: 1.2811748609999996<\/code><\/pre>\n<p>\u0412 5 \u0440\u0430\u0437 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 match\/case \u0443\u0441\u0442\u0443\u043f\u0438\u043b \u043f\u043e \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438 if\/else. \u0420\u0430\u043d\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0432\u043e\u0434\u044b, \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u0435\u0449\u0435 \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u044b\u043c\u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430\u043c\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0441\u0432\u043e\u0439 \u043a\u043b\u0430\u0441\u0441 \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u043c \u0442\u0435 \u0436\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438:<\/p>\n<pre><code class=\"python\">import timeit  # \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 class Goods:     __match_args__= ('name', 'price')     def __init__(self, name, price):         self.name = name         self.price = price          names = [\"phone\", \"TV\", \"PC\", \"car\", \"home\", \"case\", \"bird\", \"chicken\",         \"dish\", \"float\", \"C++\", \"data\", \"\"] prices = [500, 100, 1400, 2000, 750, 3500, 5000, 120, 50, 4200] goods = [] for i in range(500000):     name = names[i%len(names)]     price = prices[i%len(prices)]     goods.append(Goods(name=name, price=price))  # \u0444\u0443\u043d\u043a\u0446\u0438\u0438-\u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 def test_if(data):     for element in data:         if isinstance(element, Goods):             if element.name in [\"phone\", \"TV\"] and isinstance(element.price, int) and element.price > 2000:                 pass             elif element.name == \"case\" and isinstance(element.price, int) and element.price &lt;= 750:                 pass             elif element.name == \"case\" and isinstance(element.price, int) and element.price == 750:                 pass             elif element.name:                 pass             elif isinstance(element.price, int) and element.price > 1000:                 pass             else:                 pass def test_match(data):     for element in data:         match element:             case Goods(\"phone\"|\"TV\" as name, int(price)) if price > 2000:                 pass             case Goods(name=\"case\", price=int(price)) if price &lt;= 750:                 pass             case Goods(name=\"case\", price=750):                 pass             case Goods(str(name), _) if name:                 pass             case Goods(_, price) if price > 1000:                 pass             case _:                 pass  # \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0434\u043b\u044f \u0442\u0435\u0441\u0442\u0430 test_data = goods # \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u043e\u0432\u0442\u043e\u0440\u0435\u043d\u0438\u0439 repeats = 1000 # \u0441\u0447\u0438\u0442\u0430\u0435\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b time_repeat_if = timeit.timeit(\"test_if(test_data)\",                                setup=\"from __main__ import test_if, test_data\",                                number=repeats)  time_repeat_match = timeit.timeit(\"test_match(test_data)\",                                   setup=\"from __main__ import test_match, test_data\",                                   number=repeats) print(\"\u0420\u0415\u0417\u0423\u041b\u042c\u0422\u0410\u0422 IF\/ELSE:   \", time_repeat_if\/repeats) print(\"\u0420\u0415\u0417\u0423\u041b\u042c\u0422\u0410\u0422 MATCH\/CASE:\", time_repeat_match\/repeats) >>> \u0420\u0415\u0417\u0423\u041b\u042c\u0422\u0410\u0422 IF\/ELSE:    0.17162651800001186 >>> \u0420\u0415\u0417\u0423\u041b\u042c\u0422\u0410\u0422 MATCH\/CASE: 1.2821951220000118<\/code><\/pre>\n<p>\u0412 7.5 \u0440\u0430\u0437&#8230;<\/p>\n<h2>\u0412\u044b\u0432\u043e\u0434<\/h2>\n<p>\u042f \u043d\u0435 \u0441\u0442\u0430\u043b \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 match\/case \u043d\u0430 \u0441\u043f\u0438\u0441\u043a\u0430\u0445, \u043a\u043e\u0440\u0442\u0435\u0436\u0430\u0445, \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430\u0445 \u0438 \u0434\u0440\u0443\u0433\u0438\u0445 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430\u0445, \u043d\u0435 \u0441\u0442\u0430\u043b \u043d\u0430\u0433\u0440\u0443\u0436\u0430\u0442\u044c \u0441\u043b\u043e\u0436\u043d\u044b\u043c\u0438 \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u043c\u0438, \u0442\u0430\u043a \u043a\u0430\u043a, \u0434\u0443\u043c\u0430\u044e, \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043f\u0430\u0440\u0443 \u0432\u044b\u0432\u043e\u0434\u043e\u0432:<\/p>\n<ol>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442\u0435\u0441\u044c \u043a\u0430\u043a\u0438\u043c-\u043b\u0438\u0431\u043e \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f\u043c\u0438 \u043d\u0430 Python, \u0438\u043b\u0438 \u0432 \u0432\u0430\u0448\u0435\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u043c\u043d\u043e\u0433\u043e \u0446\u0438\u043a\u043b\u043e\u0432\/\u043c\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u043e\u043a &#8212;\u00a0<strong>\u041d\u0415<\/strong>\u00a0\u0441\u0442\u043e\u0438\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 match\/case, \u0442\u0430\u043a \u043a\u0430\u043a \u043e\u043d \u043c\u043e\u0436\u0435\u0442 \u0437\u0430\u043c\u0435\u0434\u043b\u0438\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043a\u043e\u0434\u0430 \u0432 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0437;<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u0432 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u0435\u0441\u0442\u044c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043d\u0430 \u0444\u043e\u0440\u043c\u044b \u0432\u0432\u043e\u0434\u0430, \u043b\u0438\u0431\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 if\/else \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0431\u043e\u043b\u044c\u0448\u0438\u0435, \u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u0447\u0430\u0441\u0442\u043e (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u0440\u0438 \u043a\u043b\u0438\u043a\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043d\u0430 \u043a\u043d\u043e\u043f\u043a\u0443), \u0442\u043e \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 match\/case \u043c\u043e\u0436\u0435\u0442 \u0441\u0442\u0430\u0442\u044c \u0445\u043e\u0440\u043e\u0448\u0435\u0439 \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u043e\u0439 if\/else, \u0442\u0430\u043a \u043a\u0430\u043a \u043e\u043d \u0441\u043e\u0447\u0435\u0442\u0430\u0435\u0442 \u0432 \u0441\u0435\u0431\u0435 \u043c\u043d\u043e\u0433\u043e \u0445\u043e\u0440\u043e\u0448\u0438\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 (\u0441\u043c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0435 \u0441\u0442\u0430\u0442\u044c\u0438 \u0432\u043e \u0432\u0432\u0435\u0434\u0435\u043d\u0438\u0438);<\/p>\n<\/li>\n<li>\n<p>\u0411\u0443\u0434\u0435\u043c \u043d\u0430\u0434\u0435\u044f\u0442\u044c\u0441\u044f \u0438 \u0436\u0434\u0430\u0442\u044c \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044e match\/case, \u0442\u0430\u043a \u043a\u0430\u043a \u0432\u0435\u0440\u0441\u0438\u044f Python 3.10 \u043c\u043e\u043b\u043e\u0434\u0430\u044f \u0438 \u0432\u044b\u0448\u043b\u0430 \u0442\u043e\u043b\u044c\u043a\u043e \u043c\u0435\u0441\u044f\u0446 \u043d\u0430\u0437\u0430\u0434.<\/p>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/592089\/\"> https:\/\/habr.com\/ru\/articles\/592089\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u0440\u043e\u0448\u043b\u043e \u0443\u0436\u0435 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0441 \u043c\u043e\u043c\u0435\u043d\u0442\u0430 \u0440\u0435\u043b\u0438\u0437\u0430 Python \u0432\u0435\u0440\u0441\u0438\u0438 3.10. \u0421\u0430\u043c\u044b\u043c \u0433\u043b\u0430\u0432\u043d\u044b\u043c \u0438 \u0441\u0430\u043c\u044b\u043c \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u043c \u0431\u044b\u043b\u043e \u0432\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430 match\/case (\u043e\u043d \u0436\u0435 pattern matching).\u00a0<\/p>\n<p>\u041e\u0434\u043d\u0430\u043a\u043e \u0434\u0430\u043b\u0435\u043a\u043e \u043d\u0435 \u0432\u0441\u0435\u043c \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430\u043c \u0438\u0437 \u043d\u0430\u0448\u0435\u0433\u043e \u043a\u043e\u043c\u044c\u044e\u043d\u0438\u0442\u0438 \u0437\u0430\u0448\u0435\u043b \u0434\u0430\u043d\u043d\u044b\u0439 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440. \u0421\u0432\u0438\u0434\u0435\u0442\u0435\u043b\u044c\u0441\u0442\u0432\u0443\u044e\u0442 \u044d\u0442\u043e\u043c\u0443 \u0434\u0430\u0436\u0435 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438 \u043f\u043e\u0434 \u0441\u0442\u0430\u0442\u044c\u044f\u043c\u0438 \u043d\u0430 \u0445\u0430\u0431\u0440\u0435 (<a href=\"https:\/\/habr.com\/ru\/post\/585216\/\" rel=\"noopener noreferrer nofollow\">\u0441\u0442\u0430\u0442\u044c\u044f 1<\/a>,\u00a0<a href=\"https:\/\/habr.com\/ru\/post\/585518\/\" rel=\"noopener noreferrer nofollow\">\u0441\u0442\u0430\u0442\u044c\u044f 2<\/a>), \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u044b\u043b\u0438 \u043f\u043e\u0441\u0432\u044f\u0449\u0435\u043d\u044b match\/case.<\/p>\n<p>\u041d\u0430 \u043c\u043e\u0439 \u0432\u0437\u0433\u043b\u044f\u0434, \u043d\u043e\u0432\u044b\u0439 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 \u0443\u043f\u0440\u043e\u0449\u0430\u0435\u0442 \u0436\u0438\u0437\u043d\u044c \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430\u043c, \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u044f \u043d\u0430 \u0441\u0435\u0431\u044f \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u043e\u0439 \u0442\u0438\u043f\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438\u043b\u0438 \u043f\u0440\u0438\u043d\u0430\u0434\u043b\u0435\u0436\u043d\u043e\u0441\u0442\u044c\u044e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u043c\u0443 \u043a\u043b\u0430\u0441\u0441\u0443. \u041d\u043e, \u043a\u0430\u043a \u043c\u044b \u0432\u0441\u0435 \u0437\u043d\u0430\u0435\u043c, \u0437\u0430\u0447\u0430\u0441\u0442\u0443\u044e \u0437\u0430 \u043a\u0440\u0443\u0442\u044b\u0435 \u0444\u0438\u0447\u0438, \u0432\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0435 \u0432 \u044f\u0437\u044b\u043a, \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442\u0443 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043f\u043b\u0430\u0442\u0438\u0442\u044c. \u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u0445\u043e\u0442\u0435\u043b \u0431\u044b \u043e\u0441\u0432\u0435\u0442\u0438\u0442\u044c \u0442\u0435\u043c\u0443 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430 match\/case \u0438 \u0441\u0440\u0430\u0432\u043d\u0438\u0442\u044c \u0435\u0433\u043e \u0441 \u043e\u0431\u044b\u0447\u043d\u044b\u043c if\/else.<\/p>\n<h3>\u041d\u0430\u0447\u0438\u043d\u0430\u0435\u043c<\/h3>\n<p>\u0421\u0430\u043c\u044b\u0439 \u043e\u0431\u044b\u0434\u0435\u043d\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440, \u043a\u043e\u0433\u0434\u0430 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043f\u043b\u043e\u0434\u0438\u0442\u044c \u043f\u0435\u0440\u0435\u0431\u043e\u0440\u044b if\/else &#8212; \u044d\u0442\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u0440\u0430\u0432\u043d\u0438\u0442\u044c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0441 \u043a\u0430\u043a\u0438\u043c\u0438-\u043b\u0438\u0431\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438. \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u043b\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0445 \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445:<\/p>\n<pre><code class=\"python\">import random as rnd def create_rnd_data():     words = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0,          '\u0437\u0434\u0435\u0441\u044c', '\u0434\u043e\u043c', '\u0434\u0430', '\u043f\u043e\u0442\u043e\u043c\u0443', '\u0441\u0442\u043e\u0440\u043e\u043d\u0430',          '\u043a\u0430\u043a\u043e\u0439-\u0442\u043e', '\u0434\u0443\u043c\u0430\u0442\u044c', '\u0441\u0434\u0435\u043b\u0430\u0442\u044c', '\u0441\u0442\u0440\u0430\u043d\u0430',          '\u0436\u0438\u0442\u044c', '\u0447\u0435\u043c', '\u043c\u0438\u0440', '\u043e\u0431', '\u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439', '\u0441\u043b\u0443\u0447\u0430\u0439',          '\u0433\u043e\u043b\u043e\u0432\u0430', '\u0431\u043e\u043b\u0435\u0435', '\u0434\u0435\u043b\u0430\u0442\u044c', '\u0447\u0442\u043e-\u0442\u043e', '\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c',          '\u0440\u0435\u0431\u0435\u043d\u043e\u043a', '\u043f\u0440\u043e\u0441\u0442\u043e', '\u043a\u043e\u043d\u0435\u0447\u043d\u043e', '\u0441\u0438\u043b\u0430', '\u0440\u043e\u0441\u0441\u0438\u0439\u0441\u043a\u0438\u0439',          '\u043a\u043e\u043d\u0435\u0446', '\u043f\u0435\u0440\u0435\u0434', '\u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e']     data = rnd.choices(words, k=500000)     return data<\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u043e\u0441\u0442\u044b\u0445 \u043f\u0440\u043e\u0432\u0435\u0440\u043e\u043a \u0434\u0430\u043d\u043d\u044b\u0445, \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c\u044b\u0445 \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438\u00a0<code>create_rnd_data:<\/code><\/p>\n<pre><code class=\"python\"># \u043f\u0440\u043e\u0441\u0442\u044b\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 if\/else def test_if(data):     for word in data:         if word in ['\u0434\u043e\u043c', '\u0434\u0443\u043c\u0430\u0442\u044c', '\u0447\u0442\u043e-\u0442\u043e', '\u043f\u0440\u043e\u0441\u0442\u043e']:             pass         elif isinstance(word, int):             pass         elif isinstance(word, str) and len(word) > 3:             pass         elif isinstance(word, str) and word.startswith(\"\u0434\"):             pass         else:             pass # \u0442\u0435 \u0436\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 match\/case def test_match(data):     for word in data:         match word:             case '\u0434\u043e\u043c'|'\u0434\u0443\u043c\u0430\u0442\u044c'|'\u0447\u0442\u043e-\u0442\u043e'|'\u043f\u0440\u043e\u0441\u0442\u043e':                 pass             case int(word):                 pass             case str(word) if len(word) > 3:                 pass             case str(word) if word.startswith(\"\u0434\"):                 pass             case _:                 pass<\/code><\/pre>\n<p>\u0421\u0430\u043c\u043e\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0435: \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 \u043c\u043e\u0434\u0443\u043b\u044f timeit \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c \u0432\u0440\u0435\u043c\u044f, \u0437\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0432 \u0441\u0440\u0435\u0434\u043d\u0435\u043c \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f \u043a\u0430\u0436\u0434\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f. \u041f\u0440\u043e\u0432\u0435\u0434\u0451\u043c 1000 \u0438\u0441\u043f\u044b\u0442\u0430\u043d\u0438\u0439 \u043a\u0430\u0436\u0434\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438:<\/p>\n<pre><code class=\"python\">import timeit # \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0434\u043b\u044f \u0442\u0435\u0441\u0442\u0430 test_data = create_rnd_data() # \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u043e\u0432\u0442\u043e\u0440\u0435\u043d\u0438\u0439 repeats = 1000 # \u0441\u0447\u0438\u0442\u0430\u0435\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b time_repeat_if = timeit.timeit(\"test_if(test_data)\",                                setup=\"from __main__ import test_if, test_data\",                                number=repeats)  time_repeat_match = timeit.timeit(\"test_match(test_data)\",                                   setup=\"from __main__ import test_match, test_data\",                                   number=repeats) print(\"\u0420\u0415\u0417\u0423\u041b\u042c\u0422\u0410\u0422 IF\/ELSE:   \", time_repeat_if\/repeats) print(\"\u0420\u0415\u0417\u0423\u041b\u042c\u0422\u0410\u0422 MATCH\/CASE:\", time_repeat_match\/repeats) >>> \u0420\u0415\u0417\u0423\u041b\u042c\u0422\u0410\u0422 IF\/ELSE:    0.1284590820000085 >>> \u0420\u0415\u0417\u0423\u041b\u042c\u0422\u0410\u0422 MATCH\/CASE: 0.4847222329999931<\/code><\/pre>\n<details class=\"spoiler\">\n<summary>\u0417\u0430\u0447\u0435\u043c \u0434\u0435\u043b\u0430\u044e\u0442\u0441\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 isinstance \u0432 if\/else?<\/summary>\n<div class=\"spoiler__content\">\n<p>\u0422\u0430\u043a \u043a\u0430\u043a \u0432 match\/case \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0442\u0438\u043f\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 int, str, \u0447\u0442\u043e\u0431\u044b \u0432\u0441\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0432 if\/else \u0431\u044b\u043b\u0438 \u044d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430\u043c match\/case, \u0442\u043e \u0432\u0437\u0430\u043c\u0435\u043d str, int \u0434\u0435\u043b\u0430\u044e\u0442\u0441\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 isinstance(&#8230;, str\/int). \u0422\u0430\u043a \u0436\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c, \u0447\u0442\u043e Python &#8212; \u044f\u0437\u044b\u043a \u0441 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0442\u0438\u043f\u0438\u0437\u0430\u0446\u0438\u0435\u0439, \u0438 \u043d\u0435 \u0432\u0441\u0435\u0433\u0434\u0430 \u0435\u0441\u0442\u044c \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u0441\u043f\u0438\u0441\u043a\u0435 \u0431\u0443\u0434\u0443\u0442 \u0447\u0435\u0442\u043a\u043e \u0442\u043e\u0433\u043e \u0442\u0438\u043f\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b \u043e\u0436\u0438\u0434\u0430\u0435\u0442\u0435.<\/p>\n<\/div>\n<\/details>\n<p>\u0425\u043c\u043c\u043c\u2026 \u0412 3.8 \u0440\u0430\u0437\u0430 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f match\/case \u043e\u043a\u0430\u0437\u0430\u043b\u0430\u0441\u044c \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u0435\u0435, \u0447\u0435\u043c \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c if\/else. \u041d\u043e \u043d\u0435 \u0431\u0443\u0434\u0435\u043c \u0437\u0430\u0433\u0430\u0434\u044b\u0432\u0430\u0442\u044c \u043d\u0430\u043f\u0435\u0440\u0435\u0434, \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c, match\/case \u043e\u043a\u0430\u0436\u0435\u0442\u0441\u044f \u0431\u044b\u0441\u0442\u0440\u0435\u0435 \u043f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u044b\u043c\u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430\u043c\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u0440\u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0435 \u0441\u043b\u043e\u0432\u0430\u0440\u0435\u0439.<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043b\u043e\u0432\u0430\u0440\u0435\u0439:<\/p>\n<pre><code class=\"python\">def create_rnd_data():     names = [\"phone\", \"TV\", \"PC\", \"car\", \"home\", \"case\", \"bird\", \"chicken\",             \"dish\", \"float\", \"C++\", \"data\", \"\"]     prices = [500, 100, 1400, 2000, 750, 3500, 5000, 120, 50, 4200]     goods = []     for i in range(500000):         name = names[i%len(names)]         price = prices[i%len(prices)]         goods.append({\"name\": name, \"price\": price})     return goods<\/code><\/pre>\n<p>\u041f\u0440\u043e\u0432\u0435\u0440\u0438\u043c, \u043a\u0430\u043a \u043f\u043e\u0432\u0435\u0434\u0443\u0442 \u0441\u0435\u0431\u044f if\/else \u0438 match\/case \u043f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441\u043e \u0441\u043b\u043e\u0432\u0430\u0440\u044f\u043c\u0438:<\/p>\n<pre><code class=\"python\">import timeit def test_if(data):     for element in data:         if element.get(\"name\") in [\"phone\", \"TV\"] and isinstance(element.get(\"price\"), int) and element.get(\"price\") > 2000:             pass         elif element.get(\"name\") == \"case\" and isinstance(element.get(\"price\"), int) and element.get(\"price\") &lt;= 750:             pass         elif element.get(\"name\") == \"case\" and isinstance(element.get(\"price\"), int) and element.get(\"price\") == 750:             pass         elif isinstance(element.get(\"name\"), str) and element.get(\"name\"):             pass         elif isinstance(element.get(\"price\"), int) and element.get(\"price\") > 1000:             pass         else:             pass def test_match(data):     for element in data:         match element:             case {\"name\": \"phone\"|\"TV\", \"price\": int(price)} if price > 2000:                 pass             case {\"name\": \"case\", \"price\": int(price)} if price &lt;= 750:                 pass             case {\"name\": \"case\", \"price\": 750}:                 pass             case {\"name\": str(name), \"price\": _} if name:                 pass             case {\"name\": _, \"price\": int(price)} if price > 1000:                 pass             case _:                 pass  # \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0434\u043b\u044f \u0442\u0435\u0441\u0442\u0430 test_data = create_rnd_data() # \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u043e\u0432\u0442\u043e\u0440\u0435\u043d\u0438\u0439 repeats = 1000 # \u0441\u0447\u0438\u0442\u0430\u0435\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b time_repeat_if = timeit.timeit(\"test_if(test_data)\",                                setup=\"from __main__ import test_if, test_data\",                                number=repeats)  time_repeat_match = timeit.timeit(\"test_match(test_data)\",                                   setup=\"from __main__ import test_match, test_data\",                                   number=repeats) print(\"\u0420\u0415\u0417\u0423\u041b\u042c\u0422\u0410\u0422 IF\/ELSE:   \", time_repeat_if\/repeats) print(\"\u0420\u0415\u0417\u0423\u041b\u042c\u0422\u0410\u0422 MATCH\/CASE:\", time_repeat_match\/repeats) >>> \u0420\u0415\u0417\u0423\u041b\u042c\u0422\u0410\u0422 IF\/ELSE:   0.25263675300000616 >>>\u0420\u0415\u0417\u0423\u041b\u042c\u0422\u0410\u0422 MATCH\/CASE: 1.2811748609999996<\/code><\/pre>\n<p>\u0412 5 \u0440\u0430\u0437 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 match\/case \u0443\u0441\u0442\u0443\u043f\u0438\u043b \u043f\u043e \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438 if\/else. \u0420\u0430\u043d\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0432\u043e\u0434\u044b, \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u0435\u0449\u0435 \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u044b\u043c\u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430\u043c\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0441\u0432\u043e\u0439 \u043a\u043b\u0430\u0441\u0441 \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u043c \u0442\u0435 \u0436\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438:<\/p>\n<pre><code class=\"python\">import timeit  # \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 class Goods:     __match_args__= ('name', 'price')     def __init__(self, name, price):         self.name = name         self.price = price          names = [\"phone\", \"TV\", \"PC\", \"car\", \"home\", \"case\", \"bird\", \"chicken\",         \"dish\", \"float\", \"C++\", \"data\", \"\"] prices = [500, 100, 1400, 2000, 750, 3500, 5000, 120, 50, 4200] goods = [] for i in range(500000):     name = names[i%len(names)]     price = prices[i%len(prices)]     goods.append(Goods(name=name, price=price))  # \u0444\u0443\u043d\u043a\u0446\u0438\u0438-\u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 def test_if(data):     for element in data:         if isinstance(element, Goods):             if element.name in [\"phone\", \"TV\"] and isinstance(element.price, int) and element.price > 2000:                 pass             elif element.name == \"case\" and isinstance(element.price, int) and element.price &lt;= 750:                 pass             elif element.name == \"case\" and isinstance(element.price, int) and element.price == 750:                 pass             elif element.name:                 pass             elif isinstance(element.price, int) and element.price > 1000:                 pass             else:                 pass def test_match(data):     for element in data:         match element:             case Goods(\"phone\"|\"TV\" as name, int(price)) if price > 2000:                 pass             case Goods(name=\"case\", price=int(price)) if price &lt;= 750:                 pass             case Goods(name=\"case\", price=750):                 pass             case Goods(str(name), _) if name:                 pass             case Goods(_, price) if price > 1000:                 pass             case _:                 pass  # \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0434\u043b\u044f \u0442\u0435\u0441\u0442\u0430 test_data = goods # \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u043e\u0432\u0442\u043e\u0440\u0435\u043d\u0438\u0439 repeats = 1000 # \u0441\u0447\u0438\u0442\u0430\u0435\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b time_repeat_if = timeit.timeit(\"test_if(test_data)\",                                setup=\"from __main__ import test_if, test_data\",                                number=repeats)  time_repeat_match = timeit.timeit(\"test_match(test_data)\",                                   setup=\"from __main__ import test_match, test_data\",                                   number=repeats) print(\"\u0420\u0415\u0417\u0423\u041b\u042c\u0422\u0410\u0422 IF\/ELSE:   \", time_repeat_if\/repeats) print(\"\u0420\u0415\u0417\u0423\u041b\u042c\u0422\u0410\u0422 MATCH\/CASE:\", time_repeat_match\/repeats) >>> \u0420\u0415\u0417\u0423\u041b\u042c\u0422\u0410\u0422 IF\/ELSE:    0.17162651800001186 >>> \u0420\u0415\u0417\u0423\u041b\u042c\u0422\u0410\u0422 MATCH\/CASE: 1.2821951220000118<\/code><\/pre>\n<p>\u0412 7.5 \u0440\u0430\u0437&#8230;<\/p>\n<h2>\u0412\u044b\u0432\u043e\u0434<\/h2>\n<p>\u042f \u043d\u0435 \u0441\u0442\u0430\u043b \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 match\/case \u043d\u0430 \u0441\u043f\u0438\u0441\u043a\u0430\u0445, \u043a\u043e\u0440\u0442\u0435\u0436\u0430\u0445, \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430\u0445 \u0438 \u0434\u0440\u0443\u0433\u0438\u0445 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430\u0445, \u043d\u0435 \u0441\u0442\u0430\u043b \u043d\u0430\u0433\u0440\u0443\u0436\u0430\u0442\u044c \u0441\u043b\u043e\u0436\u043d\u044b\u043c\u0438 \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u043c\u0438, \u0442\u0430\u043a \u043a\u0430\u043a, \u0434\u0443\u043c\u0430\u044e, \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043f\u0430\u0440\u0443 \u0432\u044b\u0432\u043e\u0434\u043e\u0432:<\/p>\n<ol>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442\u0435\u0441\u044c \u043a\u0430\u043a\u0438\u043c-\u043b\u0438\u0431\u043e \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f\u043c\u0438 \u043d\u0430 Python, \u0438\u043b\u0438 \u0432 \u0432\u0430\u0448\u0435\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u043c\u043d\u043e\u0433\u043e \u0446\u0438\u043a\u043b\u043e\u0432\/\u043c\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u043e\u043a &#8212;\u00a0<strong>\u041d\u0415<\/strong>\u00a0\u0441\u0442\u043e\u0438\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 match\/case, \u0442\u0430\u043a \u043a\u0430\u043a \u043e\u043d \u043c\u043e\u0436\u0435\u0442 \u0437\u0430\u043c\u0435\u0434\u043b\u0438\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043a\u043e\u0434\u0430 \u0432 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0437;<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u0432 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u0435\u0441\u0442\u044c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043d\u0430 \u0444\u043e\u0440\u043c\u044b \u0432\u0432\u043e\u0434\u0430, \u043b\u0438\u0431\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 if\/else \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0431\u043e\u043b\u044c\u0448\u0438\u0435, \u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u0447\u0430\u0441\u0442\u043e (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u0440\u0438 \u043a\u043b\u0438\u043a\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043d\u0430 \u043a\u043d\u043e\u043f\u043a\u0443), \u0442\u043e \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 match\/case \u043c\u043e\u0436\u0435\u0442 \u0441\u0442\u0430\u0442\u044c \u0445\u043e\u0440\u043e\u0448\u0435\u0439 \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u043e\u0439 if\/else, \u0442\u0430\u043a \u043a\u0430\u043a \u043e\u043d \u0441\u043e\u0447\u0435\u0442\u0430\u0435\u0442 \u0432 \u0441\u0435\u0431\u0435 \u043c\u043d\u043e\u0433\u043e \u0445\u043e\u0440\u043e\u0448\u0438\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 (\u0441\u043c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0435 \u0441\u0442\u0430\u0442\u044c\u0438 \u0432\u043e \u0432\u0432\u0435\u0434\u0435\u043d\u0438\u0438);<\/p>\n<\/li>\n<li>\n<p>\u0411\u0443\u0434\u0435\u043c \u043d\u0430\u0434\u0435\u044f\u0442\u044c\u0441\u044f \u0438 \u0436\u0434\u0430\u0442\u044c \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044e match\/case, \u0442\u0430\u043a \u043a\u0430\u043a \u0432\u0435\u0440\u0441\u0438\u044f Python 3.10 \u043c\u043e\u043b\u043e\u0434\u0430\u044f \u0438 \u0432\u044b\u0448\u043b\u0430 \u0442\u043e\u043b\u044c\u043a\u043e \u043c\u0435\u0441\u044f\u0446 \u043d\u0430\u0437\u0430\u0434.<\/p>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/592089\/\"> https:\/\/habr.com\/ru\/articles\/592089\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-394637","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/394637","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=394637"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/394637\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=394637"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=394637"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=394637"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}