0

I don't understand how I get timeit to work. I've made an example, where I want to calculate the difference in processing times. I would be eternally grateful if someone have the time to break it down for me.

Basse

def main():
    prod_nums = ['V475', 'F987', 'Q143', 'R688']

def search_fast(prod_nums):
    for item in prod_nums:
        if item == 'R688':
            return True
    return False

def search_slow(prod_nums):
    return_value = False
    for item in prod_nums:
        if item == 'R688': 
            return_value = True
    return return_value
  • 3
    I don't see much `timeit` in this question about `timeit`. – tobias_k Feb 25 '16 at 16:15
  • [Documentation](https://docs.python.org/3.5/library/timeit.html) contains complete explanation with a bunch of examples. Do you have any specific problem? – Yaroslav Admin Feb 25 '16 at 16:16
  • I've read the documentation, but I struggle to understand how I can apply it to my example. I was wondering if someone could break it down for me. I'm in my starting phase of learning Python. –  Feb 25 '16 at 16:16
  • 4
    Simplest way to use `timeit` is in IPython: Just do `%timeit search_f(['V475', 'F987', 'Q143', 'R688'])` – tobias_k Feb 25 '16 at 16:18
  • 1
    If you are using interactive an session with IPython you can just time it as `%timeit search_f(prod_nums)` (same for `search_s`). If not, this question might have lots of duplicates (i.e. https://stackoverflow.com/questions/8220801/how-to-use-timeit-module). – Imanol Luengo Feb 25 '16 at 16:18
  • 1
    Or you can do it inside your file using following code `import timeit; print(timeit.timeit('search_f(prod_nums)', globals=globals()))`. The only trick is to pass `globals` parameter, so you can access your functions. – Yaroslav Admin Feb 25 '16 at 16:26

1 Answers1

0

If you want to pass arguments to your function you might want to use timeit.Timer, but make your list global like this:

prod_nums = ['V475', 'F987', 'Q143', 'R688']

And then run this:

from timeit import Timer
t = Timer(lambda: search_fast(prod_nums))
print t.timeit() # In my case will print 0.336354970932
t = Timer(lambda: search_slow(prod_nums))
print t.timeit() # 0.374251127243

timeit is useful when you want to inspect small piece of code in your dev environment.

If your function looks like that:

def search_slow():
    prod_nums = ['V475', 'F987', 'Q143', 'R688']
    return_value = False
    for item in prod_nums:
        if item == 'R688':
            return_value = True
    return return_value

You can use timeit.timeit

import timeit
timeit.timeit(search_slow)
>>> 0.3833189010620117

This will not return any result thou, only the time it took. This is another scenario in which you can use decorator. Basically you can use timeit to tell you how much time does it take for a function to execute, much like time sample_file.py in your terminal.

Based on python docs (https://docs.python.org/2/library/timeit.html): This module provides a simple way to time small bits of Python code. It has both a Command-Line Interface as well as a callable one. It avoids a number of common traps for measuring execution times.

mrkzq
  • 188
  • 1
  • 8