2010
05.19

Which is the fastest way to do this or that? Why not try them all and benchmark! :)

All you need to do is lock at the time.clock()

# We need this for OS detection:
from platform import system as OStype
 
# Step 1: tick, tock...
from time import clock
start_time = clock()
 
 
# Step 2: ???
# (You do your procedures here.)
# You NEED to do SOMETHING. Don't leave this blank!!
 
# Here's a filler of nothingness:
from time import sleep
sleep(0.05)
 
 
# Step 3: calculate duration
timeTaken = clock() - start_time
 
# In Windows clock() returns seconds but in Linux it's milliseconds, so:
units = ["seconds" if OStype() is "Windows" else "milliseconds"][0]
 
msg = "It took "+str(timeTaken)+" "+units+" to process your code."
 
Application.LogMessage(msg) # if you're in Softimage,
# otherwise use print() or whatever.

I tried to make the snippet above as generic as possible, but if you’re using Python in Softimage, you can use XSIUtils.IsWindowsOS() for detecting Windows instead of my OStype stuff.

1 comment so far

Add Your Comment
  1. Or just make a decorator for timming your code, python is awesome :)

Spam protection by WP Captcha-Free