Performance improvement of a GH_Python script

Hello everyone,
I’m a python newbie, I’ve been struggling this week to improve the code I wrote, but I need some help.
The script I got had a very slow Anemone loop diving building floors into apartments, I replaced it with faster python loop (27 s → 5s), but I’m sure there must be a way to write this code in a more efficient way so that it takes even shorter.
I was trying to replace a for loop with list comprehension iteration, but either I did it wrong or it didn’t do the trick (it still took 5 s). Can it be that the script takes so long because I wrote it in a way that replicates gh code (using ghcomp library all the time)?
What am I doing wrong? How can I make it faster? I’ll be happy to hear any comments/advice.
Thank you in advance :slight_smile:


loop_froum.gh (57.1 KB)

Hi Natalia, welcome to the forum. It helps tremendously if you isolate the problem you are asking about (see point 3 here). In this case, internalise all the inputs to your GHPython component and delete everything else (alternatively copy/paste it into a new empty Grasshopper file). I doubt anyone will be able to open, run, and navigate this:

3 Likes

oops, I’m really sorry, I attached a wrong file,
I updated the original post now with the clean script :pray:

No worries, I figured as much :slight_smile:

I see you’re implementing ghpythonlib.components for basically everything:

I really wouldn’t implement this node-in-code namespace unless you absolutely have to (i.e. some Grasshopper functionality isn’t part of RhinoCommon, or is from some plugin that doesn’t come with an API). There are several topics on problems associated with ghpythonlib.components that you might search for. I don’t recall if performance is one (it definitely was back in the day), but I’m sure there will be some overhead involved when writing code in such a roundabout way.

Instead, consider implementing RhinoCommon directly. Here’s a post on that topic and some additonal tips to speed up your GHPythoning:

Thank you Anders, the links you attached were very helpful, I’ll try to rewrite the code in the next couple days :slight_smile: