2

I create this working function for getting time of another function:

def get_execution_time(function, args, numberOfExecTime=1):
    """Return the execution time of a function in seconds.

    """

    return round(Timer(partial(function, args)).timeit(numberOfExecTime), 5)

By the way I have a problem: I can't give multiple input (args) to the function to be timed. How can I do that? Is partial the right tool?

I tried decorator but I can't store the time which is what I need for doing some statistics.

4 Answers 4

4

If you can sacrifice ability to have default value for numberOfExecTime argument you can do it like that:

from timeit import Timer
from functools import partial

def get_execution_time(function, numberOfExecTime, *args, **kwargs):
    """Return the execution time of a function in seconds."""
    return round(Timer(partial(function, *args, **kwargs))
                 .timeit(numberOfExecTime), 5)

def foo(a, b, c = 12):
    print a, b, c

get_execution_time(foo, 1, 3, 4, c = 14)

Or you can do it like that and still have default value for numberOfExecTime:

from timeit import Timer
from functools import partial

def get_execution_time(function, *args, **kwargs):
    """Return the execution time of a function in seconds."""
    numberOfExecTime = kwargs.pop('numberOfExecTime', 1)
    return round(Timer(partial(function, *args, **kwargs))
                 .timeit(numberOfExecTime), 5)

def foo(a, b, c = 1):
    print a, b, c

get_execution_time(foo, 1, 2, c = 2)
# => 1 2 2

get_execution_time(foo, 4, 5, c = 3, numberOfExecTime = 2)
# => 4 5 3
# => 4 5 3
Sign up to request clarification or add additional context in comments.

2 Comments

You don't even have to sacrifice the default. Just omit it from the arguments list and add numberOfExecTime = kwargs['numberOfExecTime'] if 'numberOfExecTime' in kwargs else 1 as your first line.
I thinks that's almost what I did in the second variant using pop method with default value.
3

You can pass multiple arguments: args should be a tuple containing the arguments to pass.

Now, you can pass *args instead of args.

(If you need to also pass kwargs to your method, then have in get_execution_time another argument kwargs and pass **kwargs to your function)

def get_execution_time(function, args=(), kwargs ={}, numberOfExecTime=1):
    return round(Timer(partial(function, *args, **kwargs)).timeit(numberOfExecTime), 5)

3 Comments

This is the best answer using existing code. However,@KL-7's have more expressive power if you want more control over the data you receive.
I hesitated to post the same proposal as proposed by KL-7 (in addition to the one I posted) but in my eyes the version I finally decided to post is the best version because the call of this function is more straightforward because there is no ambiguity of which of the passed arguments is passed to the function and which one is an argument of the method itself...
That's why I said it had more expressive power. I never said which answer was best because it is dependent on what the asker wants.
2

I would use a timing decorator.

import time

def timeit(f):

    def timed(*args, **kw):

        ts = time.time()
        result = f(*args, **kw)
        te = time.time()

        print 'func:%r args:[%r, %r] took: %2.4f sec' % \
          (f.__name__, args, kw, te-ts)
        return result

    return timed

Using the decorator is easy either use annotations.

@timeit
def compute_magic(n):
     #function definition
     #....

Or re-alias the function you want to time.

compute_magic = timeit(compute_magic)

My blog post here has more information. http://blog.mattalcock.com/2013/2/24/timing-python-code/

Comments

0

If args is a list of arguments you can expand it with *args:

return round(Timer(partial(function, args)).timeit(numberOfExecTime), 5)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.