LabVIEW Idea Exchange

cancel
Showing results for 
Search instead for 
Did you mean: 
YoungKwahk

Build Array Ordering - New array function that can insert items at the beginning efficiently

Status: Declined

Any idea that has received less than 3 kudos within 3 years after posting will be automatically declined.

Build Array Ordering.png

 

Appending to the end of an array is much more efficient than prepending to the beginning
If you need to insert items at the beginning, insert at the end instead and use a single Reverse Array when finished

 

 

Insert items.png

How about make a new array function that can insert items at the beginning efficiently?

YoungKwahk
8 Comments
tst
Knight of NI Knight of NI
Knight of NI

I'm not sure how efficient your bottom code actually is.

 

My understanding is that reversing an array simply sets a flag that says the array is reversed, without actually moving the elements in memory. The problem is that if you now want to add an element to the END of the array, you would still have to allocate or move the elements in memory.

 

Anyway, even if this can be done, you don't need a new primitive - NI can (and should) simply put any improvements it can make straight into the existing Build Array primitive and hide it from the users.


___________________
Try to take over the world!
JackDunaway
Trusted Enthusiast

YoungKwahk, could you upload a benchmark to show the bottom is faster than the top method? You'll have to create a new post on the LabVIEW forum (Idea Exchange does not allow attachments in comments) and then cross-link back here.

crossrulz
Knight of NI

Darren had a nugget about this over a year ago.  Darren's Weekly Nugget 07/20/2009


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
Mads
Active Participant

Is there a reason why NI has not done as tst suggests (implemented this under the hood of the build array function)?

tst
Knight of NI Knight of NI
Knight of NI

First, I don't KNOW that they haven't done this, but I'm assuming they haven't and the reason is the one I gave in my reply - appending to the end of a reversed array is not any more efficient, because a reversed array isn't actually flipped in memory - LV just knows it's reversed and indexes it in reverse. Appending to it would require allocating more memory BEFORE the beginning or moving the entire array, both of which are probably not very efficient.


___________________
Try to take over the world!
Mads
Active Participant

Darrens nugget suggests that it in fact is more efficient - and implicitly that the trick has not been implemented within the build array primitive.

Chris_W1
Member

This is an interesting idea, and as far as I know has not been implemented in the array primitive. As tst suggests there may be scenarios where this speed-up creates slow-downs in downstream operations. I'd have to investigate that to say for sure.

 

As with most compiler optimizations, even simple ones, knowing if and when to apply them is usually the difficult part. This is the reason we use hard evidence as our ultimate determinator when introducing optimizations--we'd need to prove that VIs on average benefit. But that's the process we follow for all optimizations in LabVIEW.

 

Good idea,

-- Chris W

LabVIEW R&D

Darren
Proven Zealot
Status changed to: Declined

Any idea that has received less than 3 kudos within 3 years after posting will be automatically declined.