perfomance of len(List) vs reading a variable

前端 未结 4 554
名媛妹妹
名媛妹妹 2021-01-12 17:17

A similar question has already been ask Cost of len() function here. However, this question looks at the cost of len it self. Suppose, I have a code that repea

相关标签:
4条回答
  • 2021-01-12 17:39

    Function calls in python are costly, so if you are 100% sure that the size of n_files would not change when you are accessing its length from the variable, you can use the variable, if that is what is more readable for you as well.

    An Example performance test for both accessing len(list) and accessing from variable , gives the following result -

    In [36]: l = list(range(100000))
    
    In [37]: n_l = len(l)
    
    In [40]: %timeit newn = len(l)
    10000000 loops, best of 3: 92.8 ns per loop
    
    In [41]: %timeit new_n = n_l
    10000000 loops, best of 3: 33.1 ns per loop
    

    Accessing the variable is always faster than using len() .

    0 讨论(0)
  • 2021-01-12 17:40

    Using l = len(li) is faster:

    python -m timeit -s "li = [1, 2, 3]" "len(li)"
    1000000 loops, best of 3: 0.239 usec per loop
    
    python -m timeit -s "li = [1, 2, 3]; l = len(li)" "l"
    10000000 loops, best of 3: 0.0949 usec per loop
    
    0 讨论(0)
  • 2021-01-12 17:43

    Using len(Files) instead of n_files is likely to be slower. Yes you have to lookup n_files, but in the former case you'll have to lookup both len and Files and then on top of that call a function that "calculates" the length of Files.

    0 讨论(0)
  • 2021-01-12 17:54

    A few results (time, in seconds, for one million calls), with a ten-element list using Python 2.7.10 on Windows 7; store is whether we store the length or keeping calling len, and alias is whether or not we create a local alias for len:

    Store Alias n=      1      10     100
    Yes   Yes       0.862   1.379   6.669
    Yes   No        0.792   1.337   6.543
    No    Yes       0.914   1.924  11.616
    No    No        0.879   1.987  12.617
    

    and a thousand-element list:

    Store Alias n=      1      10     100
    Yes   Yes       0.877   1.369   6.661
    Yes   No        0.785   1.299   6.808
    No    Yes       0.926   1.886  11.720
    No    No        0.891   1.948  12.843
    

    Conclusions:

    • Storing the result is more efficient than calling len repeatedly, even for n == 1;
    • Creating a local alias for len can make a small improvement for larger n where we aren't storing the result, but not as much as just storing the result would; and
    • The influence of the length of the list is negligible, suggesting that whether or not the integers are interned isn't making any difference.

    Test script:

    def test(n, l, store, alias):
        if alias:
            len_ = len
            len_l = len_(l)
        else:
            len_l = len(l)
        for _ in range(n):
            if store:
                _ = len_l
            elif alias:
                _ = len_(l)
            else:
                _ = len(l)
    
    if __name__ == '__main__':
        from itertools import product
        from timeit import timeit
        setup = 'from __main__ import test, l'
        for n, l, store, alias in product(
            (1, 10, 100),
            ([None]*10,),
            (True, False),
            (True, False),
        ):
            test_case = 'test({!r}, l, {!r}, {!r})'.format(n, store, alias)
            print test_case, len(l),
            print timeit(test_case, setup=setup)
    
    0 讨论(0)
提交回复
热议问题