Grasshopper

algorithmic modeling for Rhino

I've recently had a play with trying to find an easier way of doing performance profiling within Python script blocks. From what I can tell, using timeit() and %timeit / %lprun don't work within GhPython, and installing/importing a specialised module for it seems like a bit of a pain to do on a per-component basis.

I've settled on adding a bit of boilerplate code to components that lets me wrap various lines with a stopwatch and automatically compile the results. Its reasonably unobtrusive / easy to reuse - at least compared to starting and stopping the stopwatch all over the place. But, has anyone come up with a better way of attacking this problem? 

Views: 994

Replies to This Discussion

Hi Philip

I'd like to investigate why the timeit module does not work.
It seems to be present in IronPython. I'll check this now. The nice code that you wrote can definitely work, but it would be nice to have the "out-of-the-box" option, as well.

Thanks for reporting this,

Giulio
--
Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

So, I looked online for an example of using timeit, I put it into GhPython, and it seems to work.

Could you please let me know where you experience problems?
I am attaching the example that I used.

Thanks,

Giulio
--
Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

Attachments:

Hi Guilo,

Thanks for your response. I'm not sure what my exact problem was with using timeit at the time, although having had a play with it now it seems to work. Although I think one of the problems (most likely a problem with my understanding) is how to use timeit to time defined functions within a script. For example, to time a test() function you should be able to do:

print(timeit.timeit("test()", setup="from __main__ import test"))

However that throws an error, Runtime error (UnboundNameException): global name 'setup_grid' is not defined because GhPython doesn't like importing from __main__, even though print __name__ returns __main__. Is there something with how the GhPython namespace/module/scriptcontext that I'm missing here?

Hi again Philip,

this is actually a problem that we could try to tackle in a future version.

The reason this does not work, is that there can be several "__main__" modules in the same definition, so what we did is just change the name of the module, but not register it for import finding. This is something I or somebody else at McNeel might want to look into in a future version.

I've added a bugtracker item, so this stays on the to-do list.

Thanks a lot for this report,

Giulio
--
Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

Two years later, there are some good news.

We are now able to run the timeit module in the __main__module itself, and we can circumvent the issue of not having a file to pinpoint in GhPython. We can do so using any fixed attribute in other modules, for example, our beloved 'sticky' dictionary.

Here an example.

Right now it is still impossible to refer to the current "ScriptScope" as __main__, although its name is such, and this has to do with the path being nonexistent.

However, we can run timeit.

Have a go with this,

Giulio
--
Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

Attachments:

Now that's pretty damn clever! Cheers Giulio and sticky :)

Sticky saves the day again. Thanks Guilio, I just tried it out and it works perfectly. 

Awesome, thanks!

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service