04-05-2024 02:17 PM
This runs a bit slow. Any suggestions on improving performance. Typically it will iterate between several hundred and a few thousand times.
Solved! Go to Solution.
04-05-2024 03:12 PM - edited 04-05-2024 03:15 PM
This is nearly 10x faster..
Original - 0.14s
Metod #2 - 0.01s
I would have been satisfied with 0.1s for 10,000 iterations to begin with. 😉
04-05-2024 03:29 PM
Thanks.
Although 0.1s isn't much, that loop goes inside another loop that iterates several hundred times.
04-05-2024 03:57 PM
I have always found that building a string as you do is slow. Of course that benchmark is meaningless because both version run in parallel, potentially stepping on each other's toes. Also, the SR should probably be initialized.
Currently posting by phone, will have a look later.
04-05-2024 04:09 PM
@altenbach wrote:
Also, the SR should probably be initialized.
The SR is the slow part. Remove the SR and concatenate the string like the example above, either by specifying concatenate on the edge of the for loop, or build an array and concatenate after the array, no major difference. Not as fast as the one above, but a significant speed up.
04-05-2024 08:02 PM - edited 04-05-2024 08:04 PM
Note that you can even parallelize the FOR loop. This one is significantly faster on my laptop (3ms vs 480ms).
There are also a few other alternatives in the other cases, so play around. I am sure there is some slack left.....
I took the liberty to change the Y into a simple ramp, but it really should not make a difference.
04-07-2024 12:54 PM
Can the number be statically sized wrt the number of digits? If so the string could be allocated all in one go and then replace the segments with the correct numbers.
04-07-2024 12:55 PM
Or even allocate a string all at once for the maximum possible number of digits for all entries and replace into that and trim down at the end. Eliminates all the individual string allocations that need to happen with the loop, whether its the reallocation being done for the SR every time or the individual strings that then get concatenated.
04-08-2024 04:49 AM
I've seen that many times, Strings in shift registers changing sizes are slow, i assume the memory manager doesn't like it. It's fast to make an array of strings and merge them in the end, as the examples do. Making a preallocated string and replacing content might be a good (and old school) alternative.
04-08-2024 07:31 AM - edited 04-08-2024 07:54 AM
The format into string might also be slow.
Format Into String probably parses the format string each iteration. While the string can't change in this example, I don't think the function will change into another function if the format string is a constant.
There's a chance though that concatenating a string from constants and more primitive functions is slower that parsing the format string.
This could be benchmarked, but for me refactoring Format Into String usually made things faster...