using the map function with timeit in Python
<p>I am learning python and am looking at the difference in performance between using comprehension and the map function. </p>

<p>So far, I have this code:</p>

<pre><code>import timeit

print timeit.timeit('[x for x in range(100)]',number = 10000)

def mapFunc(i):
    print i

print timeit.timeit('map(mapFunc,range(100))',number = 10000)
</code></pre>

<p>After trying this, I have managed to get the list comprehension method to work correctly using timeit, however, I would like to compare this to the map function. When doing this, I get the error that mapFunc is not defined, and I do not know how to fix this issue. Can someone please help?</p>

<p>You should <em>setup</em> the function definition via the <code>setup</code> argument:</p>

<pre><code>&gt;&gt;&gt; setup='def mapFunc(): print(i)'
&gt;&gt;&gt; timeit.timeit('map(mapFunc,range(100))', number=10000, setup=setup)
</code></pre>

<p>See the <a href="https://docs.python.org/2/library/timeit.html#timeit.timeit" rel="nofollow"><code>timeit</code> doc reference</a></p>

<p>P.S. Probably not a good idea to use <code>map</code> for printing.</p>

python performance function dictionary timeit