Featured post

new redirect for blender.org bpy docs.

http://www.blender.org/api/blender_python_api_current/ As of 10/11 november 2015 we can now link to the current api docs and not be worr...

August 19, 2012

Timing your python routines part 1

I don't often time routines, because the stuff I write mostly operates in realtime or too fast to become an issue. However, there have lately been some operations on larger data that i will want to find faster ways for. Sometimes my preconceived notion about how to write the fastest function demands testing, especially on larger data. Hence mark_time() and get_last_time()

The function overhead of calling these functions will be factored out if you stick your routines in a loop for testing, then divide the resulting time by the number of loops performed. Python standard library has a timeit import, which I personally find a little unintuitive.
import time

def get_last_time():
    if len(time_list) < 2:
        return "ERROR: must have two time entries to calculate the difference"
    return time_list[-1] - time_list[-2]    

def mark_time():
    time_list.append(time.time())    

time_list = []

# 
mark_time()
#
# your routine here 
# 
mark_time()

# {: 5g}   will give the difference to 5 decimal point accuracy
print('your routine took {: 5g}'.format(get_last_time()))

No comments:

Post a Comment

Please use Blender.StackExchange.com for python scripting questions unrelated to this post.