Evaluate vs smaller than vs C# component speed difference

HI,

I noticed there’s some differences in profiling time when it comes to using the Evaluate component vs the c# component to generate a list of true/false booleans values. See screenshot attached.

I also included a smaller than component to test and this is surprisingly even faster, can anyone elaborate as to why this is?

Side note: they all output 13824 values, the list is still small, but I’d imagine the compute time to increase exponentially when working with larger/longer lists.

The difference is in the processing of the inputs, the smaller than has a quick cast to bool (or maybe input data is already bool), C# scripting inputs are a bit special, it has a slower casting I guess, and Evaluate has a parser to convert text to operations so is expected to be much slower and also a generic input which requires casting.

2 Likes

A C# script component copies value-types data into a new list with wrapped GH_Types (before and after processing). There are ways in optimizing this. This is the major bottleneck. The next is the fact that it’s a script, which needs to be compiled. The first execution after changing the text will always be slower. So always compute a second time!

Furthermore there is something called branchless programming. Don’t use if and else if not really necessary.

FastCondition.gh (11.8 KB)

4 Likes

private void RunScript(List<System.Object> x, ref object A)
  {

    var comp = GrasshopperDocument.Objects.First(c => c.NickName == "Random X1") as GH_Component;
    var data = ((GH_Structure<IGH_Goo>) comp.Params.Output[0].VolatileData)[0];

    var valTrue = new GH_Boolean(true);
    var valFalse = new GH_Boolean(false);

    var list = new List<GH_Boolean>(data.Count);
    var cnt = data.Count;

    for(var i = 0;i < cnt;i++)
    {
      var num = ((GH_Number) data[i]).Value;
      if(num < 10.0)
      {
        list.Add(valTrue);
      }else{
        list.Add(valFalse);
      }
    }

    A = list;
  }
1 Like

Great, you took pressure from memory allocation and the GC by reusing a GH_Boolean. Of course that makes sense!

This is fantastic! There is obviously a lot of understanding here that I didn’t get from doing C# courses. Could you give a novice some pointers about how he might go about learning to code like that?