LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Speed of multiplying array on element

Hi, everyone!

 

I had to multiply the array by a correction factor and I tried different variantsI tested three variants (here theirs VI snippets): 

in place.png

 

simple replace.png

just multiply.png

The most performance variant is simple multiply (results in the attachments). So, realization of this method more optimized, but in which way? Is there more optimized access to memory or it's use some native functions?

0 Kudos
Message 1 of 8
(4,237 Views)

Hi,

 

Your three methods are not comparable.

The first two are running 1000^2 times of for loop. Of course you'll find the third one is faster.

 

I think these simple operations don't require memory allocation, so the in place element structure won't help a lot.

 

The simple the better.

 

0 Kudos
Message 2 of 8
(4,211 Views)

I just want to say, that for multiplying array on number you need to access for each element and multiply them. First and second methods make this access by index/replace element. As a result we can see that this way doesn't good compare to simply multiply. That's why I interest in way which simply multiply implements this action. 

0 Kudos
Message 3 of 8
(4,201 Views)

Hi,

 

I don't understand that with all elements having the same weight, why do you need to do them element by element.

 

The idea is always like this: if you can do it using array operation, don't do it element by element.

 

Because each LV loop interation need a minimum CPU time to operate, if you want to optimise your code, try to reduce the loop size first.

0 Kudos
Message 4 of 8
(4,192 Views)

Ok, I'll try configure my problem in another way.

 

So, we have data represented as 2D array. We want modify them. 2D array only abstraction because data in memory situated serial. My CPU has 2 core, so it has 2 ALU, which can modify 2 number simultaneously. That's why we need modify every element in single manner. Backing to abstraction we use For Loop for selection single element from array (it represents in 1 and 2 variant of solution) and compare to simple multiply (which I hope most performance) it's bad approach. And I wanted to find realization of simple multiply. As a result I find next solution:

 

best approach.png

 

0 Kudos
Message 5 of 8
(4,176 Views)

I'm interested in results of you experiments, which of the methods was the fastest on you 2-core CPU? And did LabVIEW utilize both cores to the max?

 

What happens if you split the array in two equal parts and then use simple multiply and join the two arrays?

 

I think for reliable timing efforts you should calculate the total time for the 100 runs.

 

 

Ton

Free Code Capture Tool! Version 2.1.3 with comments, web-upload, back-save and snippets!
Nederlandse LabVIEW user groep www.lvug.nl
My LabVIEW Ideas

LabVIEW, programming like it should be!
Message 6 of 8
(4,167 Views)

As I said, your comparation is not equal.

suppose each for loop takes 1ms to execute.

your first one will take 100ms for 100 loops, and your second one will take 2000*100 ms.

Because the difference of an element times a number and an array times a number is so small, that the computer cannot even tell you.

 

Your difference in result can be understanded as pure loop execution time difference, not the calculation.

Message 7 of 8
(4,157 Views)

Hey Silentium,

 

The best way to understand this is to consider what instructions you are giving the computer. In the first 2 cases, this is basically what is going on;

 

1) Find some (the first, second, third, and so on) entry in the array

2) Pull this entry out of the array and store as a single value

3) Multiply that value by 5

4) Find that entry in the array again

5) Replace the value currently in the array with the new one that was multiplied by 5

6) Repeat for each position in the array

 

Whereas in the final best case, here are the instruction;

 

1) Multiply the first entry by 5

2) Repeat for each entry of the array

 

As you can see, the instructions in the second case are much simpler. There is no searching through indices or temporary variables, the VI simply performs the operation until the computer reaches the end of the array. In a bit less technical terms, the multiplication function is a primitive VI. It has been present since the first days of LabVIEW, and many many many developers worked very hard to make it work as fast as possible no matter what you throw at it, be it array or scalar. Therefore, it is unlikely that someone will be able to develop a faster algorithm for multiplying an array by a scalar in LabVIEW, especially trying to code that algorithm in LabVIEW itself where they would necessarily have to rely on the same VI they are trying to outperform. The slower two VIs are reinventing the wheel, in a sense, by trying to do what the multiplication VI does in the first place. Hope that answers your question!

John B.
Embedded Networks R&D
National Instruments
Certified LabVIEW Developer
Message 8 of 8
(4,116 Views)