Why is an inline if/else faster than a .get() in Python?

前端 未结 1 1583
孤城傲影
孤城傲影 2021-01-17 09:52

I was reviewing some code earlier and the developer wrote an inline if/else rather than a get() to retrieve an element from a list if

相关标签:
1条回答
  • 2021-01-17 10:40

    The D.get() path includes an attribute lookup, and a method call:

    >>> import dis
    >>> D = {"a": 1, "b": 2, "c": 3}
    >>> def gt(): return D.get('a', 1)
    ... 
    >>> dis.dis(gt)
      1           0 LOAD_GLOBAL              0 (D)
                  3 LOAD_ATTR                1 (get)
                  6 LOAD_CONST               1 ('a')
                  9 LOAD_CONST               2 (1)
                 12 CALL_FUNCTION            2
                 15 RETURN_VALUE        
    

    The attribute lookup (LOAD_ATTR) especially slows things down.

    If you remove the attribute lookup (and give the in test a local to work with), the field is evened out:

    >>> def gt_fast(D_get=D.get): return D_get('a', 1)
    ... 
    >>> def ef_fast(D=D): return D['a'] if 'a' in D else 1
    ... 
    >>> timeit.timeit(gt_fast)
    0.2174091339111328
    >>> timeit.timeit(ef_fast)
    0.2139298915863037
    
    0 讨论(0)
提交回复
热议问题