Strange results of evaluating in statement for list and set by means of timeit module
<p>Trying to evaluate performance of <code>in</code> statement for both: <code>set</code> and <code>list</code>. I know that I can do it with module <code>time</code>, but I want to try <code>timeit</code> module. 
So my code is next:</p>

<pre><code>from timeit import Timer


def func_to_test(val, s):
    return val in s

if __name__ == "__main__":
    val = 346
    n = 100000
    s = set([x for x in range(n)])
    l = [x for x in range(n)]
    list_timer = Timer("func_to_test(%s, %s)" % (val, l), "from __main__ import func_to_test")
    set_timer = Timer("func_to_test(%s, %s)" % (val, s), "from __main__ import func_to_test")
    print(list_timer.timeit(100))
    print(set_timer.timeit(100))
</code></pre>

<p>The output is:</p>

<pre><code>0.1953735960000813
0.6675883569996586
</code></pre>

<p>But why <code>in</code> statement for <code>list</code> "has" better performance than <code>set</code>?
I know that it's not, but why there are these results with <code>timeit</code> module?</p>

<p>Your setup statement imports <code>func_to_test</code>, but it doesn't do anything else. Therefore, the actual timing tests are timing not only the time it takes for a membership test, but also the time it takes to create a list (for the list test) or a list <em>and</em> a set (for the set test). Create the iterable in the setup statement, and the issue disappears:</p>

<pre><code>&gt;&gt;&gt; import timeit
&gt;&gt;&gt; timeit.timeit('346 in s', setup='s=list(range(100000))')
9.412489017458922
&gt;&gt;&gt; timeit.timeit('346 in s', setup='s=set(list(range(100000)))')
0.08872845571155352
&gt;&gt;&gt; timeit.timeit('346 in s', setup='s=set(range(100000))')
0.09335872296618675
</code></pre>

python python-3.x timeit