How to get faster loading of lists in c#?

Hi all!

I’m trying to build a marching cubes algorythim, actually i’ve finished it and it is working as intended.
But the speed is bugging me.
Iso Surface from Millipede plugin “eat” 18k + values (and actually create the mesh) in realtime, profiler time almost doesn’t even pop-up!
2020-04-20 20_02_31-Window
Millipede solving a 14k vertices mesh in lesser time than a simple addition?
Probably it is just the ui messing with me.
Anyway, my compliments to @Panagiotis.M for his Millipede plugin!

Trying by moving the IsoValue slider:
If i duplicate 10x the addition component, the ui start lagging!
To have the same “lag” i have to make 16 copies of the IsoSurface component!!

Everything is telling me that IsoSurface is computing a whole mesh faster than A+B!!


My solution takes 150ms or such for the same inputs.
I’ve switched to parallel/multithread and… no change!

Then, i’ve literally removed the code inside … and the time is the same!!

Here some simple c# that make just an addition. Literally x+y.
2020-04-20 19_48_19-Window
parallel_test.gh (8.6 KB)

What? Why? How?
I’m still a rookie in c#, but can someone tell me what is going on?

How can i make grasshopper “feed” a big list of values into c# faster?

Thanks in advance!

Codes:

private void RunScript(List<double> x, double y, ref object A)
  {
  int count = x.Count;
double[] result = new double[count];
for(int i = 0;i < count;i++){
  result[i] = x[i] + y;
}
A = result;
  }

and (with using System.Threading.Tasks;)

private void RunScript(List<double> x, double y, ref object A)
  {
int count = x.Count;
double[] result = new double[count];
Parallel.For(0, count, (i) => {
  result[i] = x[i] + y;
  });
A = result;
  }

I guess in this case implementing System.Linq.Enumerable.Select is faster than the methods you’ve been using.

private void RunScript(List<double> x, double y, ref object A)
{
   A = x.Select(number => number + y);
}

It seems using grasshopper’s specific types (in this case GH_Number) helps to get a little faster:

private void RunScript(List<double> x, double y, ref object A)
{
   A = x.Select(number => new GH_Number(number + y));
}

But as you’ve mentioned the most time consuming part is where the C# Script component get the input list. in this case (and other similar cases) getting the list as objects, could help a lot:

private void RunScript(List<System.Object> x, double y, ref object A)
{
   A = x.Select(number => new GH_Number((double) number + y));
}

Creating a compiled version could help getting even faster:

protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
{
    pManager.AddNumberParameter("x", "x", "x", GH_ParamAccess.list);
    pManager.AddNumberParameter("y", "y", "y", GH_ParamAccess.item);
}
protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
{
    pManager.AddNumberParameter("A", "A", "A", GH_ParamAccess.list);
}
protected override void SolveInstance(IGH_DataAccess DA)
{
    var x = new List<double>();
    DA.GetDataList(0, x);
    var y = double.NaN;
    DA.GetData(1, ref y);
    var result = x.Select(number => new GH_Number(number + y));
    DA.SetDataList(0, result);
}


Speed.gh (17.5 KB)
Speed.gha (6 KB)
Speed.zip (26.7 KB)

8 Likes

Oh, ok… WOW!
So many info all toghether! Your reply is gold for me!
Thanks!

I need to fully inderstand your samples before going forward…
Is that for Visual Studio?
May I know if it is for a specific version or else… ? I’ve used it few to nothing…

1 Like

you’re welcome. yes I’m using Visual Studio Community 2019
(Version 16.5.4) it’s free to download and use (for many scenarios). but VS was so good in backward and forward compatibility if I’m not mistaken it’s safe to use VS 2017 or 2015 with the attached project.

2 Likes

Marchin_Cubes
Great!
With your advices and some more data management (pre-allocated arrays instead of lists, etc… ) i managed to get really close times (Millipede’s Iso Surface still seems faster)
From 150+ ms to under 10ms … code optimization is something spectacular… it make you wonder how greedy are modern cpus! And i’m on c# on gh on rhino on windows…

Next step, VB!

4 Likes