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.
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:
Should the component update whenever the file changes? I.e. does the script need to be smart enough to re-run itself?
Will there be lines in file which are not valid numbers? I.e. does the script need to handle failures gracefully?
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.
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.”
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..
@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
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..
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):
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!