09-22-2020 11:39 AM
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.
Solved! Go to Solution.
09-22-2020 12:09 PM
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.
09-22-2020 12:09 PM
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". 😉
09-22-2020 12:09 PM
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!
09-22-2020 12:19 PM
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.
09-22-2020 12:21 PM
@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. 😉
09-22-2020 12:33 PM
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
09-22-2020 12:36 PM
@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...
09-22-2020 12:39 PM
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?)
09-22-2020 12:48 PM
Hi,
Please find attached VI where input array is having default values.
The output is correct but takes around 5 minutes.