LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Efficient Int32 Array to Boolean Array to Int16 Array

Solved!
Go to solution

Have a large array of Int32 (~500,000 elements).

Each Int32 needs to be parsed into two Int16 arrays: Array 1 is the first 12 bits of the Int32, Array 2 is the second 12 bits of the Int32.

The method I am using (see attached) works just fine but it is taking a lot of time to run (on a relatively slow, embedded CPU).

 

Does anyone have an idea how to make this run faster?

 

 

0 Kudos
Message 1 of 12
(4,203 Views)

The best format in which to post an image is PNG (JPEG and GIF are ok, too).  Please do not embed an image in an RTF or Word document.

 

Here's one quick approach, haven't tested the timing but I suspect it's faster.  There's probably room to optimize further.

I32 to two 12-bit U16.png

0 Kudos
Message 2 of 12
(4,183 Views)
Solution
Accepted by RedSoxFanatic

Using boolean operations on numbers appears to be quite a bit faster: 42 ms vs 280 ms for 1000000 point arrays.  See the image below.

 

Also, when I duplicated the code in your image I did not get the results you described in your post.  Please post images as .png files.  They can be embedded rigtht into the post using the insert/edit image button (looks like a tree).

 

Lynn

 

Twelve bits.png

0 Kudos
Message 3 of 12
(4,180 Views)

Glad I refreshed.  That was exactly what my snippet looks like.  And agreed, I have not benchmarked it.  And not sure there is a more optimal way of doing it.


Paul
0 Kudos
Message 4 of 12
(4,177 Views)

Thanks everyone - that works great.

0 Kudos
Message 5 of 12
(4,156 Views)

The polymorphism of boolean inputs is one of those little things which people often miss when they are learning to use LV.  They can take both scalar and array inputs.

 

Lynn

0 Kudos
Message 6 of 12
(4,153 Views)

Since you have large arrays, the above methods allocate a few extra arrays, possibly slowing you down.

 

Here's a version that uses fewer array allocations. You might want to benchmark a few versions....

 

0 Kudos
Message 7 of 12
(4,134 Views)

Excellent - thanks I'll give it a try!

0 Kudos
Message 8 of 12
(4,112 Views)

@red Sox Fanatic wrote:

Excellent - thanks I'll give it a try!


Not that you needed more ways to solve this problem, but lots of us on this board are curious about performance and I had a few minutes to throw together a VI to test the timing of a few approaches suggested here, as well as two variations of an additional approach that I like (shown here).  The basic conclusion is that they're all pretty close on my machine (LV 2009).  Splitting a 1-million element array, averaged over 50 times, the approach with 2 ANDs was 20ms.  Using a FOR loop was 21ms.  Using a pair of shifts (shown below) was 17ms.  A variation of this, with a typecast, was much slower, at 35.5ms.

 

2x12 bits from 32.png

0 Kudos
Message 9 of 12
(4,092 Views)
I am currently posting via phone so I cannot look at your code in detail. Two things: (1) don't place controls or indicators inside the timing frames. (2) make sure you disable debugging. (I don't think snippets retaain this setting). Also compare memory footprints.
0 Kudos
Message 10 of 12
(4,080 Views)