Link to home
Create AccountLog in
Avatar of johneanderson
johneandersonFlag for United States of America

asked on

Performance of platform invoke

I am using platform invoke from a C# program to make driver calls into an external DLL.  One of the driver calls returns a large number of samples from a buffer in the driver (see code snippet).  I have done some time measurements around this call and found that it takes a very long time to return (~200-400 msec).  In each case, I'm reading about 128K of data (128K signed integers).

I make a number of other driver calls in this DLL using the same mechanism, but they return instantly.  These other calls however, do not return large buffers.

My questions are:

1) Is the marshaling of the return int[] array causing a performance bottleneck?  
2) If so, can I use different marshaling attributes to improve performance?
3) Do I need to write my own unmanaged C++ class and wrap it in a managed C++ class to do the work of retrieving this buffer?
// driver call declaration
        [return: MarshalAs(UnmanagedType.U4)]
        private static extern
            uint GetSamples(IntPtr handle,
            [In][MarshalAs(UnmanagedType.LPArray)] int[] buffer,
            [MarshalAs(UnmanagedType.U4)] uint size);
// method which uses driver call:
        /// <summary>
        /// Get TMSI samples
        /// </summary>
        /// <param name="handle">Device handle of open device</param>
        /// <param name="buffer">Buffer to place result</param>
        /// <param name="size">Size of buffer, in 32-bit samples. One sample
        /// is for all channels. If, for example there are 16 channels, the 
        /// buffer size must be a multiple of 16.</param>
        /// <returns>Number of samples read</returns>
       public static uint GetTMSISamples(IntPtr handle, int[] buffer, uint size) {
            try {
                // convert size in 32-bit words to size in bytes
                size *= 4;
                // get samples
                uint sizeRead = GetSamples(handle, buffer, size);
                // convert size returned from bytes to 32-bit words & return
                sizeRead /= 4;
                return sizeRead;
            catch (Exception ex) {
                throw new DriverIfcException("TMSI: GetSamples() fails", ex);

Open in new window

Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account