Managed function vs unmanaged function

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello everybody!

I have a question. Anybody knows why managed function calls faster in MC++
rather than unmanaged function (defined with #pragma umanaged)? The
differences is about 4 times slower.

Thanks for your answers!
Z-Mechanic.
 
Could you send us some code with compiler switches you used so that we can
do performance comparisons ?
For eg, is the function you are trying to call virtual ? Is it called from a
managed callsite ? Did you compile it pure?
Thanks,
Kapil
 
It can be down to managed / unmanaged transitions, depend on what's going on
inside the function, and lots of other factors. If you could post the
function along with its calling context, someone will be able to give you a
definitive explanation. We didn't actually find a significant performance
hit when we eradicated all the "#pragma unmanaged" directives from our maths
code, and we found that performance could be greatly affected by unnecessary
managed/unmanaged transitions. Unless you've got a really good reason to use
#pragma unmanaged, and unless that chunk of code is fairly large and doesn't
call back into managed code, you might be better leaving it all as managed
code.

Steve
 
Hi!

I have a math library that makes 3D matrices (4x4) multiplication. After
compillation it placing in same assembly. When I compile that source with
#pragma unmanaged it works 2 times faster rather tham managed. It's fine.

But... Simple test. Source code of test files listed below. Simply call
TestCPP::Class1::Test(0);

---- Begin of TestCPP.h ----
// TestCPP.h
#pragma once

using namespace System;

int vvv(int i);

int vvv1(int i);
int vvv2(int i);
int vvv3(int i);
int vvv4(int i);
int vvv5(int i);

namespace TestCPP
{
public __gc class Class1
{
public:
static void Test(int v)
{
int m = 0;

for (int j = 0; j < 100; j++)
for (int i = 0; i < 1000000; i++)
{
m = vvv( i );
// m = vvv1( i );
// m = vvv2( m );
// m = vvv3( m );
// m = vvv4( m );
// m = vvv5( m );
}

m++;
}
};
}
---- End of TestCPP.h ----

---- Begin of TestCPP.cpp ----
// This is the main DLL file.

#include "stdafx.h"

#include "TestCPP.h"

#pragma unmanaged

int vvv(int i)
{
i = vvv1( i );
i = vvv2( i );
i = vvv3( i );
i = vvv4( i );
i = vvv5( i );

return i;
}

int vvv1(int i)
{
i++;
return i;
}

int vvv2(int i)
{
i++;
return i;
}

int vvv3(int i)
{
i++;
return i;
}

int vvv4(int i)
{
i++;
return i;
}

int vvv5(int i)
{
i++;
return i;
}
#pragma managed
---- End of TestCPP.cpp ----
 
Hi!

I have a math library that makes 3D matrices (4x4) multiplication. After
compillation it placing in same assembly. When I compile that source with
#pragma unmanaged it works 2 times faster rather tham managed. It's fine.

But... Simple test. Source code of test files listed below. Simply call
TestCPP::Class1::Test(0);

---- Begin of TestCPP.h ----
// TestCPP.h
#pragma once

using namespace System;

int vvv(int i);

int vvv1(int i);
int vvv2(int i);
int vvv3(int i);
int vvv4(int i);
int vvv5(int i);

namespace TestCPP
{
public __gc class Class1
{
public:
static void Test(int v)
{
int m = 0;

for (int j = 0; j < 100; j++)
for (int i = 0; i < 1000000; i++)
{
m = vvv( i );
// m = vvv1( i );
// m = vvv2( m );
// m = vvv3( m );
// m = vvv4( m );
// m = vvv5( m );
}

m++;
}
};
}
---- End of TestCPP.h ----

---- Begin of TestCPP.cpp ----
// This is the main DLL file.

#include "stdafx.h"

#include "TestCPP.h"

#pragma unmanaged

int vvv(int i)
{
i = vvv1( i );
i = vvv2( i );
i = vvv3( i );
i = vvv4( i );
i = vvv5( i );

return i;
}

int vvv1(int i)
{
i++;
return i;
}

int vvv2(int i)
{
i++;
return i;
}

int vvv3(int i)
{
i++;
return i;
}

int vvv4(int i)
{
i++;
return i;
}

int vvv5(int i)
{
i++;
return i;
}
#pragma managed
---- End of TestCPP.cpp ----
 
This is related to exactly what I said; in your managed call, you go from
calling one unmanaged function to calling 5. When they're wrapped into a
single unmanaged function in the DLL, there's less managed -> unmanaged
transitions, and less DLL overheads. There was a nother thread on here a few
days ago about all this, and there's lots of info on the net.
I can't see much advantage making this unmanaged, to be honest - I doubt
you'll see a big performance increase.

Steve
 
Hi!

Greate thanks for explanations!

It was a test only to check perfomance on procedure calling. As I said
before I got a great increase of perfomance on a complex math functions when
I put my code to unmanaged.
 
Hi,

Yes, typically with complex instructions you will find performance
increases. It's just important to ensure that you're not constantly
transitioning between managed and unmanaged code, or you'll lose the
benefits. I'm sorry I can't find the explanation of all this; if you Google
for something like "Unmanaged managed code transition" you'll probably get
some info.

Steve
 
Back
Top