@Alons

# Почему это работает быстрее?

Почему это
``````def fast_string_comparison(brand, product_name):
product_name = product_name.split()
exact_match_brand = list(set(brand) & set(product_name))
if exact_match_brand:
exclusive_matches_brand = set(brand) ^ set(exact_match_brand)
else:
exclusive_matches_brand = brand
return exact_match_brand, exclusive_matches_brand``````

работает быстрее чем это
``````def fast_string_comparison(brand, product_name):
product_name = product_name.split()
exact_match_brand = list(set(brand) & set(product_name))
if exact_match_brand:
exclusive_matches_brand = set(brand) ^ set(exact_match_brand)
return exact_match_brand, exclusive_matches_brand
else:
return exact_match_brand, brand``````
• Вопрос задан
• 104 просмотра
Пригласить эксперта
Ответы на вопрос 1
@PashaWNN
Для того, чтобы понять, как это работает на низком уровне, можно использовать функцию `dis.dis`

первый вариант:
``````>>> from dis import dis
>>>
>>> def fast_string_comparison(brand, product_name):
...         product_name = product_name.split()
...         exact_match_brand = list(set(brand) & set(product_name))
...         if exact_match_brand:
...             exclusive_matches_brand = set(brand) ^ set(exact_match_brand)
...         else:
...             exclusive_matches_brand = brand
...         return exact_match_brand, exclusive_matches_brand
...
>>> dis(fast_string_comparison)
4 CALL_METHOD              0
6 STORE_FAST               1 (product_name)

14 CALL_FUNCTION            1
20 CALL_FUNCTION            1
22 BINARY_AND
24 CALL_FUNCTION            1
26 STORE_FAST               2 (exact_match_brand)

30 POP_JUMP_IF_FALSE       50

36 CALL_FUNCTION            1
42 CALL_FUNCTION            1
44 BINARY_XOR
46 STORE_FAST               3 (exclusive_matches_brand)
48 JUMP_FORWARD             4 (to 54)

7     >>   50 LOAD_FAST                0 (brand)
52 STORE_FAST               3 (exclusive_matches_brand)

8     >>   54 LOAD_FAST                2 (exact_match_brand)
58 BUILD_TUPLE              2
60 RETURN_VALUE``````

Второй:
``````>>> from dis import dis
>>> def fast_string_comparison(brand, product_name):
...         product_name = product_name.split()
...         exact_match_brand = list(set(brand) & set(product_name))
...         if exact_match_brand:
...             exclusive_matches_brand = set(brand) ^ set(exact_match_brand)
...             return exact_match_brand, exclusive_matches_brand
...         else:
...             return exact_match_brand, brand
...
>>> dis(fast_string_comparison)
4 CALL_METHOD              0
6 STORE_FAST               1 (product_name)

14 CALL_FUNCTION            1
20 CALL_FUNCTION            1
22 BINARY_AND
24 CALL_FUNCTION            1
26 STORE_FAST               2 (exact_match_brand)

30 POP_JUMP_IF_FALSE       56

36 CALL_FUNCTION            1
42 CALL_FUNCTION            1
44 BINARY_XOR
46 STORE_FAST               3 (exclusive_matches_brand)

52 BUILD_TUPLE              2
54 RETURN_VALUE

8     >>   56 LOAD_FAST                2 (exact_match_brand)
60 BUILD_TUPLE              2
62 RETURN_VALUE
66 RETURN_VALUE
>>>``````
Ответ написан
Ваш ответ на вопрос

### Войдите, чтобы написать ответ

Похожие вопросы
от 130 000 ₽
deeplay Новосибирск
от 130 000 ₽
Enjoy PRO Санкт-Петербург
от 140 000 до 180 000 ₽
26 окт. 2020, в 01:56
3500 руб./за проект
25 окт. 2020, в 23:21
5000 руб./за проект
25 окт. 2020, в 21:47
1500 руб./за проект