LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

For loop is taking too muc time to execute

Solved!
Go to solution

Hello,

 

I am converting U8 array to U16 array by performing some operations o the incoming array using for loop.

Size of input U8 array is 5308416 and for loop is running 884736 times. Now this for loop takes around 5 minutes to generate output U16 array.

Can anyone please suggest other way or optimize the current logic to reduce this time ?

The VI snapshot is attached herewith. 

Thanks in advance.Capture.PNG

0 Kudos
Message 1 of 26
(2,416 Views)

You can try initializing your shift register to the appropriate size, enabling parallelism on the For loop, and changing your Insert Into Array function to a Replace Array Subset function.

 

I'm not sure if this will have an effect, but you can swap your Unsigned Byte Array to a U64 in 1-shot before it goes into the For loop so you don't have all the repetitive type changes to U64 inside.

0 Kudos
Message 2 of 26
(2,395 Views)

Attach a VI where your data in the controls is saved as default.

 

No one is going to go through the trouble to recreate your picture.

 

What are you actually trying to do here?

I see a lot of repeated code elements.  It feels like it could be simplified.

 

I'm most suspisicious of the Insert Into Array.  A For Loop running nearly a million times means there will be multiple resizings of that array as it grows.  Initializing an array before the For Loop and replacing elements would certainly help.

 

With a VI to work on and a basic idea of what you are doing, Altenbach will likely give you a better version that runs 1000 times faster and "fits on a postage stamp".  😉

Message 3 of 26
(2,394 Views)

I am not going to analyze a page full of overly complicated code, so can you describe in a few words what the input represents and how it should be transformed into the output. Thanks!

 

Message 4 of 26
(2,393 Views)

It seems the first half is basically taking a subset of 6 bytes, padding it to 8 bytes while casting it to U64 (Hint: that's all you really need). (index array is resizeable, etc.)

 

In the second half, you seems to shift and mask certain parts before reassembling (again way too much duplicate code! How many times do you need to shift the same number by the same amount?? (-8, 32, etc.).

 

All you probably need is a concatenating output tunnel. No shift register needed.

 

Yes, I am sure the final code can fit on a postage stamp an execute quickly.

0 Kudos
Message 5 of 26
(2,369 Views)

@altenbach wrote:

I am not going to analyze a page full of overly complicated code, so can you describe in a few words what the input represents and how it should be transformed into the output. Thanks!

 


This is step 1 to altenbach creating code that is 1000x faster and fits on a postage stamp.  😉

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 6 of 26
(2,367 Views)

Hi,

I am converting raw U8 array to RAW12 format. Attached VI for reference.

Input array (U8 stream)
10
32
54
76
98
BA
DC
FE
11
Output array (RAW12 out)
2100
5430
8760
BA90
EDC0
11F0

0 Kudos
Message 7 of 26
(2,354 Views)

@altenbach wrote:

It seems the first half is basically taking a subset of 6 bytes, padding it to 8 bytes while casting it to U64 (Hint: that's all you really need). (index array is resizeable, etc.)


Reshape Array before the loop would help here where we can reshape the 1D array into a 2D array with each row containing 6 elements.  Then we can autoindex on the 2D array!  Then a Type Cast to change the U8 array into a U64 inside of the loop.

 


@altenbach wrote:

In the second half, you seems to shift and mask certain parts before reassembling (again way too much duplicate code! How many times do you need to shift the same number by the same amount?? (-8, 32, etc.).


Looks to me like it is trying to form 12-bit numbers into U16 with the least significant 4 bits set to 0.  I'm thinking a simple FOR loop to shift right the U64, do a simple mask, and shift left again move the desired 12 bits back up to the MSb of the new U16.

 

Time to start playing...


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 8 of 26
(2,351 Views)

Can you show an input where the size is divisible by six?

 

(Just add maybe 12 typical elements to your input array, make current values default, then save and attach again. Can we assume that your code is just slow, but otherwise gives the correct result?)

0 Kudos
Message 9 of 26
(2,346 Views)

Hi,

Please find attached VI where input array is having default values.

The output is correct but takes around 5 minutes.

0 Kudos
Message 10 of 26
(2,340 Views)