radikyl
radikyl

Reputation: 11

List Membership Check vs For Loop Performance

Related to this post: If in List vs For loop (performance)

While timing the execution, test1() is at least twice as slow as test2(). There are fewer lines in test2(). Is it due to this? Besides it and also 'in' being a pythonic way of doing things. Is 'in' mostly more efficient than similar loop structure, and should it be preferred over loops for list and dict checks for performance reasons?

import timeit

a = [n for n in range(1, 1000)]
s = a[len(a) - 1]

def test1():
    for i in a:
        if i == s:
            break

def test2():
    if s in a:
        pass

if __name__ == '__main__':
    n = 10000

    print("test1 milli-sec per loop: ", timeit.timeit("test1()",
                                                      setup="from __main__ import test1", number=n) / n * 1000)
    print("test2 milli-sec per loop: ", timeit.timeit("test2()",
                                                      setup="from __main__ import test2", number=n) / n * 1000)

Upvotes: 0

Views: 227

Answers (1)

iz_
iz_

Reputation: 16583

The code the two are executing are essentially the exact same, with one major difference: in pushes the loop to the C level (assuming CPython), while the second loop is very clearly Python.

C obviously executes faster than Python, giving you the performance difference. In general, if you only want to check for membership (True or False), use the first option, but if you want to do something more complex, like getting the index (with a loop with enumerate) and doing further operations, use the second. For small-ish lists, the time difference is so insignificant that you can freely use either form.

Upvotes: 1

Related Questions