I’ve created a fairly extensive C# component (from my perspective at least, +600 lines of code), which I want to turn into a plug-in at a later stage. The component is set up to create a list of a couple of 100K text elements, basically a text file.
What is some common practice advice in C# programming (for GH in particular) that will automatically make the script perform better?
For example, I use a lot of for loops on lists to do an operation on each element. Is that considered efficient or the opposite?
Another example, embedding if statements deep down into a number of for loops instead of on a higher level. If you can limit the number of calculations by duplicating code and first run the if statement for one condition and then for the other, will that be more efficient? Less calculations = faster resulting script? What is the best way to skip parts of code and decrease the amount of calculations that are useless otherwise? (Such as multiplying by a variable that is set to 0)
I’ve read somewhere that for loops over Arrays are far more efficient than over List? I don’t know the programming behind list operations with standard GH components, so I wonder if there is another way to loop through a list of objects? I’ve created a List which I continuously fill with string elements with the .Add() method. I then write the entire list to the output at the end of the script.
Is using global variables considered good or bad? What about creating custom functions as much as possible (didn’t do that at this point)?
Also, when developing the plug-in at some point, is it wise to create custom classes and use them to my advantage, if suitable? Is that more efficient or does the performance not increase?
Lastly, what is more efficient: just 1 all-in-one component that takes a lot of inputs and then gives you the output OR an assembly component that takes a couple of other input components (a more visual scripting, connecting components approach).
I have a background in structural engineering, so my programming knowledge is more intuitive than computationally efficient. So until now, if it did what it should had done, it worked for me.
Some general tips are welcome at this point. I can still rewrite the code at this stage, if required for huge performance improvements.
I’m not able to share the code unfortunately, as it is part of an ongoing research project.