Python speed test

Hi All,

Is there a way to run a speed test on a python node to understand how long each section of code is taking to run? I have a node with around 1200 lines of code and I’d like to understand where it can be improved to speed it up. I know there’s the Profiler widget, but that only tells me the total time, not the time for each line/section

Thanks,

Matt

I usually just import time and do something like this:

Though there are more advanced standard Python profiling modules as well.

Edit: Here’s a quick implementation (I usually import the Timer from this module):

201112_GHPythonTimer_00.gh (2.0 KB)

Edit 2: There’s also the .NET Stopwatch class (known from C#/VB):

from System.Diagnostics import Stopwatch

sw = Stopwatch()
sw.Start()

foo = 0
for i in range(100000):
    foo+=1

sw.Stop()
print sw.Elapsed
2 Likes

Also check out the timeit module to get more accurate measurements for simple snippets.

Graham.

2 Likes

Awesome, that’s just what I needed, thanks!

Out of interest, do you know why there’s a discrepancy between the timer duration and what the profiler shows?

The only code I have before the timer starts is a few imports and the definition of the timer itself:

Your example has a difference of around 33%(10ms) which isn’t so bad, but mine is out by almost double (30ms)

1 Like

That’s likely due to the cost of inputting and outputting data (i.e. setting appropriate type hints on the inputs, and wrapping data in appropriate types on the outputs):

1 Like