After some many fun coding / learning hours and thanks to the awesome community here I was able to create my first time ever C# Script component that basically replicates a definition I already created in grasshopper using regular components (see attached)
What the component does is take a brep gemetry and creates a line (that passes through the center of the brep.) with the same or reduced length.
The problem that I have is that my C# script is way too slow compared with the grasshopper definition. I am not totally sure what is the bottleneck in my C# script that is causing such big performance issues with only 10 breps.
For comparison you can see that the GH definition is so amazingly fast!
I am not entirely sure what could be causing such a big performance difference. I attached my GH file and if someone can take a look at it and provide some feedback to be able to make my C# script run as fast as the GH definition it will be great.
The time-consuming step is the calculation of the volume centroid. In your GH definition, you have parallel execution enabled, whereas in the C# code this is not computed in parallel.
You can do it in parallel using System.Threading.Tasks namespace and Parallel.For execution. Then the c# block becomes as fast as the parallel-enabled centroid block in the definition.
Add to the top bit:
using System.Threading.Tasks;
Then replace the centroid calculation with this:
Point3d[] centerOfGravity = new Point3d[_Breps.Count];
Parallel.For(0, _Breps.Count, i =>
{
Rhino.Geometry.Point3d pointCG = VolumeMassProperties.Compute(_Breps[i]).Centroid;
centerOfGravity[i] = pointCG;
});
You must use an array for the centerOfGravity results instead of a Point3dList. The list is not thread safe, and the order of the results becomes unpredictable due to parallel execution.
The calculation of the centroids can be further speeded up by only calculating the volume and the first moments, and not the second or product moments: