I have created my first Python script! I wanted to compare the computational speed between coding with python and coding with the visual components of GH. I have coded the same problem using the same components and in this case, I obtained visual programming is faster. How is it possible? I thought it was just the opposite…Is there a way to optimise the python script done?
Moreover, I have duplicate four times the python script, giving four different computational times. Why is that happens? Shouldn’t it take the same time?
Measuring and improving performance is not trivial. The whole system affects the computation, based on what your computer does next to your task. There are dozens, if not hundreds, of active background processes running on a Windows instance. So you will never get the same result.
Furthermore, scripting is not always faster, because you inject code into a system. So piping of data in and out of your script is always less-optimized than from a native component. Then you are not going to do the same procedures as the original component does, and the layer of access might also differ. Plus on the first execution the code needs to be compiled and loaded.
Rhinocommon is a wrapper around native Rhino functionality, and Rhinoscriptsyntax is a wrapper around Rhinocommon. Any layer on top comes with its own little performance costs.
You can only improve performance if you make something better than a native component, by improving the algorithm underneath. Often this is possible because a concrete use-case allows you to make some assumptions, or you simply combine multiple operations into one, reducing the amount of loops. On top of that, you can implement some language features like parallel computation or using more efficient data structures.
In the end, a performance gain below a magnitude is not worth the effort. The real benefit of scripting is getting a higher level of control, better readability and better handling of edge cases. If you want to write well performing code, you really need to know what you are doing.