Задать вопрос
@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
  • Вопрос задан
  • 107 просмотров
Подписаться 1 Средний 3 комментария
Пригласить эксперта
Ответы на вопрос 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)
  2           0 LOAD_FAST                1 (product_name)
              2 LOAD_METHOD              0 (split)
              4 CALL_METHOD              0
              6 STORE_FAST               1 (product_name)

  3           8 LOAD_GLOBAL              1 (list)
             10 LOAD_GLOBAL              2 (set)
             12 LOAD_FAST                0 (brand)
             14 CALL_FUNCTION            1
             16 LOAD_GLOBAL              2 (set)
             18 LOAD_FAST                1 (product_name)
             20 CALL_FUNCTION            1
             22 BINARY_AND
             24 CALL_FUNCTION            1
             26 STORE_FAST               2 (exact_match_brand)

  4          28 LOAD_FAST                2 (exact_match_brand)
             30 POP_JUMP_IF_FALSE       50

  5          32 LOAD_GLOBAL              2 (set)
             34 LOAD_FAST                0 (brand)
             36 CALL_FUNCTION            1
             38 LOAD_GLOBAL              2 (set)
             40 LOAD_FAST                2 (exact_match_brand)
             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)
             56 LOAD_FAST                3 (exclusive_matches_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)
  2           0 LOAD_FAST                1 (product_name)
              2 LOAD_METHOD              0 (split)
              4 CALL_METHOD              0
              6 STORE_FAST               1 (product_name)

  3           8 LOAD_GLOBAL              1 (list)
             10 LOAD_GLOBAL              2 (set)
             12 LOAD_FAST                0 (brand)
             14 CALL_FUNCTION            1
             16 LOAD_GLOBAL              2 (set)
             18 LOAD_FAST                1 (product_name)
             20 CALL_FUNCTION            1
             22 BINARY_AND
             24 CALL_FUNCTION            1
             26 STORE_FAST               2 (exact_match_brand)

  4          28 LOAD_FAST                2 (exact_match_brand)
             30 POP_JUMP_IF_FALSE       56

  5          32 LOAD_GLOBAL              2 (set)
             34 LOAD_FAST                0 (brand)
             36 CALL_FUNCTION            1
             38 LOAD_GLOBAL              2 (set)
             40 LOAD_FAST                2 (exact_match_brand)
             42 CALL_FUNCTION            1
             44 BINARY_XOR
             46 STORE_FAST               3 (exclusive_matches_brand)

  6          48 LOAD_FAST                2 (exact_match_brand)
             50 LOAD_FAST                3 (exclusive_matches_brand)
             52 BUILD_TUPLE              2
             54 RETURN_VALUE

  8     >>   56 LOAD_FAST                2 (exact_match_brand)
             58 LOAD_FAST                0 (brand)
             60 BUILD_TUPLE              2
             62 RETURN_VALUE
             64 LOAD_CONST               0 (None)
             66 RETURN_VALUE
>>>
Ответ написан
Комментировать
Ваш ответ на вопрос

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

Похожие вопросы
23 дек. 2024, в 16:13
50000 руб./за проект
23 дек. 2024, в 15:25
5000 руб./за проект
23 дек. 2024, в 14:47
4500 руб./за проект