LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Improving this loop

Here is a little challenge..  I need to improve the performance of this loop.
 
There are other parts to this loop, but I'd like to concentrate on the core functionality.
 
Please note that this is part of a software upgrade, which has already been dramatically improved.  Plus the order is to avoid a complete re-design...  They are the some of the constraints I am working with.  So far, I managed to reduce the overall time of the test from a few hours to 20 minutes on a slow machine (Pentium-466).
 
Here is what the loop shown below does.
 
It receives a multi-line string that was read from a file.
The string looks like this:
 
1 0 1 0 0 1 1 0 1 1 0 0 1 0 0 1
1 0 1 0 0 1 1 0 1 1 0 0 x x 0 0
1 0 1 0 0 1 1 0 1 1 0 0 1 X X X
1 0 1 0 0 1 1 0 1 1 0 0 X 1 1 1
1 0 1 0 0 1 1 0 1 1 0 0 0 X x 1
1 0 1 0 0 1 1 0 1 1 0 0 0 0 0 0
etc
where the X or x are don't care conditions.
 
The file may contain approx 25,000 lines of the above.
 
The loop converts the file contents to binary array and (string to byte array).  The two arrays are needed for content verification later on..  I may change it, but that's beyond the scope of this exercise for now.  The don't cares have been changed to zero's (0) as part of a design constraint.
 
Since I have an approach for later on, I have created an array of don't cares (not shown).
 
This test is run multiple times using multiple different vectors (file contents).
 
I need to convert the file contents to the 2 arrays faster.  It is now my only bottleneck.. 
 
Being head deep into this code (originally created by someone else) may impede me from finding a more refined and faster solution.
 
I'm sure someone has a good example to speed this up..  Maybe removing the While Loop alltogether..  😄 
And it could be a Nugget in the making.. 
 
Thanks!
 
RayR
 
 
0 Kudos
Message 1 of 30
(2,941 Views)

Hi Ray,

Please post the code!

First thought:

The Selects do not execute until both imputs are satisfied.

In the image you posted, this means that the work required for both select inputs as to be done even though you only want the one of the results.

By moving the code into a case, you can control which set of operations are done.

Ben

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
Message 2 of 30
(2,913 Views)
RayR,

i sadly dont have the time now to take a closer look into the issue, but there are some points coming up in mind now:

Question: The string you describe should "display" 16 bit and then /n and the next 16 bit?

Improvements:
a) Remove the typecast by changing the numeric constant to representation U8.
b) Replace While loop with a For loop. N is the length of the string. Therefore, you wouldn't have dynamic memory reallocs.

These are my first points, have fun!

Norbert B.

PS maybe i will get back to this later 🙂
Norbert
----------------------------------------------------------------------------------------------------
CEO: What exactly is stopping us from doing this?
Expert: Geometry
Marketing Manager: Just ignore it.
Message 3 of 30
(2,911 Views)
Ray,

I'd also look to get away from the while loop to a for loop using an array of lines (pre-process the string into an array of strings).  You could also use "search string for tokens" to get the lines without the line ends and then simply build one big string (If we know the final string size, we can insert instead of appending) which can be fed into a "to U8".  At the end, re-shape the array.

Oh, and something so obvious it might be terribly wrong - take out the Wait function.

Shane.
Using LV 6.1 and 8.2.1 on W2k (SP4) and WXP (SP2)
Message 4 of 30
(2,904 Views)
"Oh, and something so obvious it might be terribly wrong - take out the Wait function."
 
Ben slaps forehead and utters "Doh!"
 
Well at least make the wait "0" if there is other stuff running at the same time this code is running.
 
Ben
Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
Message 5 of 30
(2,898 Views)
Hmm... I think you simply should convert your initial string to a byte string and then pass this byte string to a for loop with automatic indexing. Add a case structure to the for loop and connect your indexed byte to the case selector "?" terminal. Create case "88,120" for x and X, "48" for 0 and "49" for 1 and default for any other characters that you simply ignore. Now you have a fast for loop with a case for each input type you may have in your array.

Second add an ampty I8 array to a new shift register of the loop. In your case structure, for cases 48 and 49 add value 0 or 1 to this array respectively. For case "88,120" add  value -1 to this array. This way you end up in a 1d array with values from set {0, 1 ,-1 } depending on what you have in your text file.

Then after the loop use reshape array to make your array a 2D array of the shape of your initial data. If you still want to make a separate boolean array, wire you 2D I8 array to "<0" node and 2D boolean array you have.

This will perhaps outspeed your VI by a factor of 100-1000. If this still is not fast enough for you, initialize your array empty array to the expected size before connecting it to the shift register. You can guess the size from the size of the string. Then instead of adding new elements to the array use replace array element so that all array operations happen in place in memory.

Tomi


--
Tomi Maila
Message 6 of 30
(2,896 Views)

Wow!  Thank you guys.

Some excellent responses!

I knew I forgot to mention something.  The "= empty string" was the only way to detect the end of the file, as the file size varies (as well as contents).

I wish I could post the actual code... Maybe I can post a subset, especially after trying out what was proposed.  🙂

For the "Selects", they both deal with their own arrays.  I am implementing a change later in the code where I may be able to get away from the bottom one altogether.  I really don't need two arrays representing nearly the same thing..  (original code had nth arrays!!  😮 )

I'm going to investigate what Shane & Norbert proposed.

------------------

Question: The string you describe should "display" 16 bit and then /n and the next 16 bit?
Yes, something like that..  each line consists of qty 16: "1", "0", and "X" all seperated by spaces.  These are created by another computer program.

Improvements:
a) Remove the typecast by changing the numeric constant to representation U8.
This change (constant of 48) to a U8 only improved the duration by 0.5 second.  However, still a good practice, which I need to remember!  😉

b) Replace While loop with a For loop. N is the length of the string. Therefore, you wouldn't have dynamic memory reallocs.

I agree...  The problem is that I do not know how many lines there are ahead of the loop.  How would I count the no. of \n 's?

OK... I read this suggestion again..  You would loop for the number of ASCII characters within the file..  WOW that's a lot!!

25000 (lines) * ( 16 (bits) + 15 (spaces) ) = 775,000 iterations

Of course, I could remove all whitespace first..  25000 * 16 = 400,000.

You mean that it would go fatser with 400,000 iterations of a For Loop than 25,000 iterations of a While Loop?  😮

🙂

 

0 Kudos
Message 7 of 30
(2,888 Views)

Joe,

It may be interesting to see a speed comparison, but how well do the shipping digital VIs work?  There is the DWDT Spreadsheet String to Digital, which will give you a nice digital waveform including the Xs.  Depending on your code further on it may be easier to use DWDTs anyway.

I glanced into the VI and I think you could speed it up some as well.  It may be a nice excersize.

Message 8 of 30
(2,887 Views)

Hi Evan,

Another thing I forgot is that I am using LV7.1.. 

What Digital VI's??  Did this exist for LV7.1 (DWDT Spreadsheet String to Digital)? 

I was thinking of doing something in line with Tomi's suggestion.. 

BTW,  so far, there is a 25% improvement.  Another 25% and I will be happy!  🙂

 

0 Kudos
Message 9 of 30
(2,877 Views)
Hi,
perhaps I got the question wrong, but my solution would be:




I tested it on a 2,4 GHz P4 and it took nearly no time (0,5 secs perhaps) with about 30k lines of your sample data pasted into the string control.
Message 10 of 30
(2,874 Views)