Slow performance when converting Text to Numbers

Hello,
I have a large amount of Numbers that I get from text document that I load into Grasshopper.
Converting the text to Numbers seems to be rather slow:
Converting 30000 Text objects to numbers takes more than 1 second.


100000 text objects take 3.5s.
These numbers change more than once a second so the conversion is a bottleneck in my definition.
I am also planning on using even more numbers (up to a million) so this is a significant performance issue.
Is there some sort of best practice when converting between text and number types?
I will probably script my own text file parser to extract the numbers directly but I was wondering if there is an easier way without scripting.
Thank you for any help or ideas.

Could you share the definition or at least part of it that handles the number creation?

Also, do you have string/text characters like letters in your original text values or just numbers?

Can you not use numbers from the get go, instead of passing text between whatever is generating the numbers and the number result?

It will take some fixed time per conversion. I’m not sure how much that is but that’s your lower bounds. What you can try is to remove the overhead imposed by Grasshopper.

The most obvious thing (although unless you’re a programmer not exactly a trivial thing) to try is to read the file using a custom script component and perform the conversion directly. Possibly even using multiple threads to do so.

Two things are important to know:

  1. Should the component update whenever the file changes? I.e. does the script need to be smart enough to re-run itself?
  2. Will there be lines in file which are not valid numbers? I.e. does the script need to handle failures gracefully?

I got it down using straight up C#. Nearly 70k numbers in 17 milliseconds.
fast number parser.gh (13.6 KB)

This will require a Grasshopper which has the new scripting components. I tried using the legacy ones, but they’re crashing now. Here’s the script in case you need it:

  private void RunScript(string File, out object Numbers)
  {
    var lines = System.IO.File.ReadAllLines(File);
    var numbers = new GH_Number[lines.Length];

    for (int i = 0 ; i < lines.Length; i++)
      numbers[i] = new GH_Number(double.Parse(lines[i]));

    Numbers = numbers;
  }

The two main optimisations here are to side-step the line-by-line handling of the data between the first few components, and to directly create GH_Numbers instead of doubles, replacing two rather expensive conversions with a single fast one.

Do note that double.Parse() will throw exceptions if the text isn’t a valid number, so if your file contains empty lines or comments or some other non-numeric data that will have to be handled.

3 Likes

Any chance to use this component under Rhino 7?
Thank you!

You could try it without asking. :roll_eyes:

In theory! But in practice is not possible as I don’t know coding. I’ve tried to upload the definition in Grasshopper under Rhino 7 and it doesn’t work. I thought this is the reason for which @DavidRutten said “This will require a Grasshopper which has the new scripting components. I tried using the legacy ones, but they’re crashing now.” :smiling_face:

Me neither, regarding C#. I’m only a dabbler in Python, and Python isn’t as fast as C#, but this is seven times faster than the OP reported.

numbers = []
for line in Lines:
    numbers.append(float(line))
Numbers = numbers


Python number parser.gh (7.6 KB)

P.S. Or is it only four times faster?

1 Like

Here for Rhino7 - but be warned, the real problem here is not the parsing, but passing around data as long lists - depending on what you do, C# might be the better choice, especially with GPT4 here to help you..

TextParser.gh (6.1 KB)

1 Like

@Joseph_Oster Thank you very much - it helps me a lot! According to my simulation, in my case where I can reach 330.000 lines it is 4 time faster than Number and 2 times faster than Dale’s Gbetti.gh

@atair It would be possible to have as an input a Panel? I would like to test it!
Thank you!

With an input panel it wont be any faster than the python version you have from @Joseph_Oster - the limiting factor at this point is moving the data inside grasshopper, not processing it..

One can speed things up further by applying a few performance tips:


250509_PythonNumberParser_00.gh (8.1 KB)

The same tips apply to the C# scripting component I believe, which would likely make things even faster. Also I’m using a map/lambda expression here, but the generator/comprehension method on line 3 is pretty much as fast. Edit: here’s a performance comparison of different approaches to the looping (i.e. where the map/lambda implementation is slightly fastest in this case):


250509_PythonNumberParser_01.gh (1.6 MB)

1 Like

Wow! Sorry if I don’t understand what you are saying. I said I’m a novice, and I don’t know coding. But I’m always amazed at the help the Rhino community offers—one of the reasons I’ve chosen and will stay with Rhino! I’m sure other people will understand what you are saying and, like me, will find your advice helpful!
Here is my comparison: from 28.7s to 1.3s is speechless for me!
Many thanks to all contributors to this thread - it was very helpful for me!

1 Like