The Daily CLAD

Community Browser
cancel
Showing results for 
Search instead for 
Did you mean: 

CLAD2017 - Fundamentals - Performance Building an Array

Active Participant

You have a requirement to build an array of random U8 numerical values using a loop. Put the following methods and loop types  in order of performance (quickest to slowest)?

 

a) For loop using "auto indexing"
b) For loop using "build array" and a Shift Register
c) While loop using "auto indexing"
d) While loop using "build array" and a Shift Register

Combined.png

Comments
Knight of NI

A C B D


There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
Member

A, C, B, D

Member

can it be C A D B  ??????

Knight of NI

umer777,

No, A is definitely the most efficient.  The reason is because a FOR loop has a fixed number of iterations.  So the Autoindex tunnel has a preallocated array.

The While Loop with the autoindex is next because of how the compiler incrementally preallocates more memory for the output tunnel.  Put simply, it doubles the memory when the allocated memory is used up.

B and D I would say are about the same since the Build Array forces a memory reallocation each time it is ran.


There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
Member

So I had a go and got the following timings for making an array of 100,000 U8 numbers (in seconds)

 

a) 0.013

c) 0.015 (which is pretty quick, I thought this would have taken longer)

b) 0.030

d) 0.033

 

I guess the difference between b&d is code I added to decide when to stop the while loop?

 

Knight of NI

I just did a benchmark of generating 1M random doubles for each method and averaged over 100 iterations.  I got the following average times:

FOR Autoindexing: 22.52ms

While Autoindexing: 25.17ms

FOR Shift Register: 122.82ms

While Shift Register: 280.99ms

 

NOTE: LabVIEW 2016 32-bit, made sure debugging was turned off


There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
Member

ACbd

Member

I can’t offer a detailed explanation. But my assumption was the same: A For loop should be faster than a while loop (more work to do with comparisons, open end till condition) and auto index should be faster than build function (build function seems to me a more universal and complex function). Second reason seems more significant than first reason. So A-C-B-D

Active Participant

my gut feeling says A, C, B+D

i know that A must be the fastest, because the array will be allacoted in the proper size since it is known how big it must be,

B second because i guess there will be some fancy optimization to allocate array ressources efficiently.

 

B+D should be somewhat equal, since in each step the new array has to be allocated.

 

EDIT: oh nice .. mostly right, although i am now curious how crossrulez' B and D differ by a factor of 2, i would have guessed timsrize's result to be the one. any explanations for the difference?


If Tetris has taught me anything, it's errors pile up and accomplishments disappear.
Member

Hello guys!

 

Following the techniques taught in Core l and Core ll, I would say the correct answer is:

A, C, B, D;

See you soon. 

Tarciso Junior
+55 (11) 996-282-103
Skype: Tarciso.junior2
Email: Tsjabrantes@gmail.com
http://br.linkedin.com/pub/tarciso-junior/23/a83/463
Member

ACBD

Thanks & Regards,
Bharath Kumar