Why is System.Object input faster than double in a C# script

I was running a test with a C# component based on a blog I saw some time ago.
I am using a simple for loop that finds the maximum in a list of numbers. In the first case, the input is an object while in the second case I defined it as a double. There is a huge difference in the time it takes to run the script; around 20 times faster, in the case that the input is defined as System.Object.

I was wondering why is this happening, and is it better if in general, the input is System.Object instead of a double, Point3d, Curve, or something else.
Test c# script.gh (4.0 KB)

I’ve seen things like this multiple times, and my guess was that for some reason casting from one type to another made by Grasshopper makes the time difference. E.g. take these values and put it into the Number container - you can see already that container itself will need a lot of time to compute.

Generally for large data sets Grasshopper can be pretty slow, one workaround is to not pass large lists of values between components, but to pass e.g. one object that contains some large list.

1 Like

int is a struct. Passing a struct in a by-reference input like the one that Grasshopper uses, requires boxing. This slowdown is likely due to boxing + unboxing, but this is only my guess.

1 Like

I think the majority of the slowdown is caused by a call to value.GetType().GUID on each value of the list during the casting to the hinted type. I think its also called twice per value on the list. If no hint is supplied no casting is done.