2

I have some C# code that requires extensive binary manipulation, so I wrote an unmanaged C++ method to replace one of the C# methods. To my shock, is was 10 X slower. I ran a profile, and discovered the slowness comes from the overhead of calling the external method, not the method itself.

So I thought that if I write the method in managed C++, I will loose the overhead of the call, but still have the speed of C++. First, is this assumption valid?

Here is my unmanaged C++ code:

#include "stdafx.h";

unsigned __int32 _stdcall LSB_i32(unsigned __int32 x)
{
    DWORD result;
    _BitScanForward(&result, x);
    return (unsigned __int32)result;
}

Here is my C# code:

public static partial class Binary
{
    [DllImport(@"CPP.dll")]
    public static extern int LSB_i32(int value);
}

Am I doing anything wrong here?

How do I translate the above to managed C++? I did some browsing on this, but because I am unfamiliar with managed C++, I didn't get far.

IamIC
  • 17,747
  • 20
  • 91
  • 154

2 Answers2

2

You could leave the unmanaged method but you should not call it very often from managed code. For example if you are calling the unmanaged method in a tight loop then the overhead of marshaling between managed and unmanaged code will be enormous. So you could try putting this loop inside the unmanaged code so that you perform only a single call to the method. Then you will pay the marshaling price only once and the whole heavylifting will be done in unmanaged code.

As far as converting it to managed C++ is concerned, I highly doubt this would bring you anything better than what you started with (i.e. fully managed C# code).

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Unfortunately, I can't put the code inside the loop as it is a helper function. The C++ code of what I am doing should be a single ASM function (bsr) at its core, which should be notably faster than the complex C# binary manipulation. If nothing else, I'd like to try. – IamIC Dec 25 '11 at 09:32
  • 1
    @IanC, in this case, leave it as fully managed code. It will run much faster. – Darin Dimitrov Dec 25 '11 at 09:33
  • you mean fully managed C# code? Surely if it's managed, I should be able to write managed C++ and benefit from it? – IamIC Dec 25 '11 at 09:39
  • 1
    @IanC, no, IMHO you won't benefit from managed C++. It runs in the CLR and you will get similar performance as C#. – Darin Dimitrov Dec 25 '11 at 09:40
  • does managed C++ have all the benefits of regular C++ such as SSE and ASM? – IamIC Dec 25 '11 at 09:43
  • @IanC, I don't think that you can compile SSE in managed code. – Darin Dimitrov Dec 25 '11 at 09:46
0

You can try if a pure C# implementation is faster:

static int lzc(int x)
{
    x |= (x >> 1);
    x |= (x >> 2);
    x |= (x >> 4);
    x |= (x >> 8);
    x |= (x >> 16);

    x = x - ((x >> 1) & 0x55555555);
    x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
    x = (x + (x >> 4)) & 0x0f0f0f0f;
    x = x + (x >> 8);
    x = x + (x >> 16);
    return 32 - (x & 0x0000003f);
}
Codo
  • 75,595
  • 17
  • 168
  • 206
  • Thanks. I have a much faster implementation than this in C#. What I want to know is how to write the above code in managed C++. – IamIC Dec 25 '11 at 10:15