Timeit timing a python function
<p>I am trying to time the below function, however it shows me the error, can't import name val_in_range, what's the error, is there any other method to do it better? </p>

<pre><code>import timeit

x = 10000000

def val_in_range(x, val):
    return val in range(x)

print (val_in_range(x,x/2))

timeit.timeit( 'val_in_range(x,x/2)', 'from __main__ import val_in_range, x',  number=10)
</code></pre>

<p>Output:</p>

<pre><code>True
Traceback (most recent call last):
  File "python", line 11, in &lt;module&gt;
  File "&lt;timeit-src&gt;", line 3, in inner
ImportError: cannot import name 'val_in_range'
</code></pre>

<p>replace
<code>timeit.timeit( 'val_in_range(x,x/2)', 'from __main__ import val_in_range, x',  number=10)</code></p>

<p>with <code>timeit.timeit(lambda:val_in_range(x,x/2), number=10)</code></p>

<p>you can print the value directly using the <code>print</code> statement.</p>

python python-3.x