08-21-2014 07:32 AM
Hello.
I want to take a file and remove every line that starts with an '!' and save the file to a single string.
After some hours of work and a lot of googling around, I finally implemented something that works, but it is way too slow, it needs about 2 seconds for a 1140 lines file.
It is obvious that the way I am handling the string is awful, but I cannot find another way to do it.
Can anyone help me with tips and tricks to make it work a lot faster?
I am using LabVIEW 2014, because it is new, I am also attaching a saved version of 2011.
Thanks everyone.
Solved! Go to Solution.
08-21-2014 07:43 AM
I dealt with a similar issue a few weeks ago. The reason your code is running slowly is likely becasue it is building the string as it goes. This means that every time you build the string, you request a new chunk of memory which is slow. LV sometimes recognises this and optimises things for you, but it might not be doing that now.
A faster way would be to make a large empty array at the begining, replace values instead of adding them, then remove the remaining empty values at the end. Hope that helps!
08-21-2014 07:49 AM - edited 08-21-2014 07:50 AM
First, I would read the entire file in one shot. You are using a lot of time constantly opening and closing that file. That is likely a very large percentage of your time. From there, use a FOR loop and do your check.
08-21-2014 07:58 AM
That conditional thing there is pretty neat. Is that new in 2013?
Also, what is that second block? I don't recognize it.
08-21-2014 08:06 AM
@awood04 wrote:
That conditional thing there is pretty neat. Is that new in 2013?
Also, what is that second block? I don't recognize it.
The conditional terminal was new with 2012.
That second block is actually the Concatenate String reduced to just 1 element. When you use the Concatenate String on an array of strings, it just concatenates all of the elements of the array into a single string.
08-21-2014 08:18 AM
Amazing!
It is way too fast now and the cool thing is that it saved me some time from another formatting I wanted to do.
I am also a bit confused now. When am I going to learn how to write that efficient code myself?
Thanks again.
08-21-2014 08:26 AM
Cool trick with the concatenate strings block!
I usually end op writing unbearably slow code, then thinking of a better way the next day either while showering or drinking my morning coffee.
08-21-2014 08:47 AM
vakost wrote:
I am also a bit confused now. When am I going to learn how to write that efficient code myself?
What are you confused about? Anything we can shed some light on?
Efficient code comes with experience. I've been learning these tricks for almost 10 years now...and still learning.
08-21-2014 08:51 AM
Forgive me, English is not my mother tongue.
What I mean is not confused but rather frustrated because I could not even come close to the solution you posted.
Again, I am only writing code for some months now.
Cheers!
08-21-2014 08:53 AM
vakost wrote:
Again, I am only writing code for some months now.
Like I said, it takes time. I was much worse off than you when I was using LabVIEW for just a few months.