BranchCount slow in C#

While scripting, I’ve noticed that if I use .BranchCount, the C# script would take a lot longer, especially if the data set it large, while the tree statistics and param viewer can easily do the same thing without much problems.

Is this behaviour normal? or is this some kind of bug?
Any way to count the branches in C# as effectively as tree stat or pram viewer?

This might be related to the cost on inputting large amounts of data to scripting components (especially with type hints). You could try turning the type hint off on your x variable.

interesting read, turning off hinting has significantly decrease computation time by about 4~5 times, it’s now around 50ms instead of around 200ms for 15,000 branches.

still tho, tree stat and param viewer is more efficient, why is that?
I’d assume tree stat uses the same code? is it simply just cause it’s already compiled as a gh component?

I’m not 100% sure, but in my experience compiled (gha) components do not have the problem that C# components have with casting.

Other possible factors:

  • The second time you execute a C# component it will always be faster (since it has to compile the code the first time)
  • I’m not sure if compiled components measure performance exactly the same way C# components do.

I see.
in regards to casting, how do I do it for lists and data trees?
If it’s a single item, I can do (int) x
but if I try something like (int) x[i] in list or data tree, I’d get errors