[TriLUG] a python question

Igor Partola igor at igorpartola.com
Mon Jan 20 20:58:03 EST 2014


Assuming you know that the RPi has no real time clock and ntpd will adjust system time for you, making these results potentially inaccurate (just average lots of them to avoid it), here is a simple decorator that will do this in your one big script:

import time

def profile(func):
    def wrapper(*args, **kwargs):
        s = time.time()
        res = func(*args, **kwargs)
        delta = time.time() - s

        # inset delta and func.__name__ into a database or a log file

        return res

    return wrapper

Use it as follows:

@profile
def my_command():
    pass

Note that time.time() returns a float so you will get microseconds. I do not know what precision clocks you get on an RPi so the resolution might not be down to microsecond level.

Out of curiosity, what are you benchmarking?

Igor


More information about the TriLUG mailing list