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
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:
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: