LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

C# .NET Based DLL slower than LabVIEW - Any suggestions ?

All

I have a routine that I wrote in LabVIEW using formula node. I wrote the same routine in C# to create a DLL. For some reason the LAbVIEW routine seems to be much faster than the .NET dll by about 1 second. I am really not sure what's causing this issue ? I am attaching my C# sample code...

        public static void demo(ref int[,] PP, int arraySize,int[,] CRM)
        {
            int i, j, temp1, temp2;
            for (i = 0; i < arraySize; i++)
            {
                temp1 = CRM[i, 0];
                temp2 = CRM[0, i];

                for (j = 0; j < arraySize; j++)
                {
                    if (temp1 != CRM[i, j])
                    {
                        PP[i, j] = 1200;
                        temp1 = CRM[i, j];
                    }
                    if (temp2 != CRM[j, i])
                    {
                        PP[j, i] = 1200;
                        temp2 = CRM[j, i];
                    }

                }
            }
 
        }


I've attached a snapshot of my VI which calls this DLL. Please do let me know if i am doing something wrong or missing something.

Thanks.


Kudos are the best way to say thanks 🙂
0 Kudos
Message 1 of 8
(4,492 Views)
Maximus00,

I would think that calling a .NET DLL would increase the amount of time it takes for communication, but 1 second seems like a lot of additional time.  Have you tested the DLL in a C# program to see if it takes around the same amount of time there?  I don't see anything wrong with how you are calling the DLL, but I would make sure that the execution time is actually being spent running the DLL using highlight execution.  I would not expect to see any of those other VIs causing a delay, but it could be. 

Please let me know if you have any additional information.
Andy F.
-----------------------------------------------------------------
National Instruments
Message 2 of 8
(4,470 Views)
Hello Andy

I found couple of things. I do know that the processing time if allocated for the DLL. I did not include this in my previous post.

I tried using this DLL in a c# code and it seems to be way faster. There is some memory leak while calling into LabVIEW or I guess something else needs to be done to sync the speeds in c# and LabVIEW. Since LabVIEW can execute the formula node faster than C# DLL, is why I am telling this.

Any suggestions ?

Kudos are the best way to say thanks 🙂
0 Kudos
Message 3 of 8
(4,456 Views)
As Andy said, I can't see a full second difference showing up based on the architecture, but there are a variety of things that can happen, from memory copies to JIT compiling of the .NET code, creation of the AppDomain, etc. I have some questions...
 
1. What version of LV are you using?
2. What version of .NET are you using?
3. How are you making the measurements? .NET is particular has a higher startup cost than a formula node, so the first call is always going to be more expensive.
3. Could you post the VI for the formula node? I'm assuming the VI image you showed is a simple VI that just has 256 data elements.
 
If you can give me the VIs in question, I can try to recreate the situation here and see where the overhead is.
 
Thanks
 
 
0 Kudos
Message 4 of 8
(4,445 Views)
1. What version of LV are you using?
LabVIEW 8.0.1
2. What version of .NET are you using?
2.0
3. How are you making the measurements? .NET is particular has a higher startup cost than a formula node, so the first call is always going to be more expensive.
I am passing in two 256x256 I32 arrays to the .NET. To be frank all .NET calls seem to take the same time..:(
4. Could you post the VI for the formula node? I'm assuming the VI image you showed is a simple VI that just has 256 data elements.
 
Please find attached the VI. I've written this VI to execute for both the formula node and the .NET dll.
 
One thing, I am using some DLLS compiled from LabWINDOWS and some from C # .NET. Do you think that might cause problems ?
 

Kudos are the best way to say thanks 🙂
0 Kudos
Message 5 of 8
(4,437 Views)
Okay, here are the results...and they make sense to me. Please let me know if anything I say doesn't make sense to you.
 
I changed the example you sent to make a seperate one for .NET and for the formula node - to keep everything clean. I also ran one pass before starting the timer (to get all the JIT, page faults, etc out of the way) and then looped 25 times and reported the average. Here is what I got on my machine
 
.NET, with CRM output wired: 378.92 ms
 
.NET, with CRM not wired: 268.92 ms
 
Formula Node (1000 iterations) 0.041 ms
 
So, where is the time being spent? Memory copies.
 
1. The first big hit you're going to take is having to copy the data in and out of .NET. The larger the array, the larger the memory copy.
2. The next really big hit (probably the biggest) is using 2D arrays with .NET. 1D arrays we can do a memory copy internally and get really good performance. In the 2D case, the .NET array isn't the same memory layout of a non-.NET array, so we must copy element by element...ouch!
3. Internally, .NET is much slower at accessing 2D array elements - again, it's an issue with their array design. Even MS admits the multi-dimensional arrays in .NET need work.
4. When using formula nodes, all of this goes away - LV has full access to the memory and no copies need to be made. Everything is pure assembly code manipulating the data.
 
If you can convert your code to work with a linear 1D array (each row one after another in a 1D format), you'll probably see the time drop considerably. Still faster in LV - all one environment with no copies at all, but the difference would probably be down to a few tens of ms at most.
Message 6 of 8
(4,433 Views)

Lycangeek

You were absolutely right. I reshaped my 2-D to 1-D and I saw the speed go up by about 40-50%. Yes, as you said there is a speed difference between LabVIEW and .NET dll by about 100-200 ms but I can handle that. Thanks a lot ! LabVIEW rocks !!


Kudos are the best way to say thanks 🙂
Message 7 of 8
(4,415 Views)
*whew!*
 
He shoots, he scores 🙂
Message 8 of 8
(4,409 Views)