Specific instances where native code runs faster

H

Henri.Chinasque

Hi all,

I am wondering if there are any quick/efficient ways to look at a
piece of c++ code and determine if it would run faster if it was
compiled to native code. I ask this because all of my c++ is currently
compiled with the /clr flag meaning that nothing is compiled native.

I've seen some examples where wrapping arithmetically intense code
with "pragma unmanaged" results in increased performance, but this was
in test code with possibly oversimplified non "real world" code.

At first I thought this would be something simple, but after a bit of
reading it seems to become enormously complex with the following
themes recurring:

compiler options
allowing for JIT "warmup"
improper benchmarking code that doesn't result in definitive speed
increase

etc.

Any articles, tips, comments would be much appreciated.
 
D

David Lowndes

I am wondering if there are any quick/efficient ways to look at a
piece of c++ code and determine if it would run faster if it was
compiled to native code.

I suspect the differences of just recompiling as you have been would
be quite small.

You need to find some real situation in your application that performs
poorly before bothering.

To see significant performance differences you'd probably need to
rewrite the offending code to take advantage of an improved algorithm
(which may just be doing things like eliminating heap allocations
inside loops).

Dave
 
H

Henri.Chinasque

I had figured as much... I was lazily hoping that there could be a
blanket way to easily speed up my code. thanks.

What I'm now struggling to understand is if I could incur performance
penalties by calling an unmanaged function (converted to unmanaged
with the #pragma unmanaged statement) from a managed function. I
wouldn't have thought so, but I'm not 100% sure.

thanks,
HC
 
D

David Lowndes

What I'm now struggling to understand is if I could incur performance
penalties by calling an unmanaged function (converted to unmanaged
with the #pragma unmanaged statement) from a managed function. I
wouldn't have thought so, but I'm not 100% sure.

There is some overhead in transitioning, but it will depend on how
often you're doing it as to whether it'd be significant in your
situation.

Have a look at the MSDN topic titled "Performance Considerations for
Interop (C++)" for more information. It's rather sketchy but I don't
recall reading any in-depth documentation of the thunking involved.

Dave
 
B

Ben Voigt [C++ MVP]

David said:
There is some overhead in transitioning, but it will depend on how
often you're doing it as to whether it'd be significant in your
situation.

And most of the overhead is dealing with data. If you're passing built-in
types like int or double, virtually no cost at all. Pinning arrays will be
a little more expensive, and much more if they are still pinned during a
garbage collection. Marshalling COM objects will probably be the worst.
 
H

Henri.Chinasque

Hi guys,

thanks for your responses, a big help. I then got curious and used
reflector to have a look at the differences between native classes
preceded by pragma unmanaged or managed. The funny thing is that I
couldn't see any differences. I get either

this: (preceded by #pragma unmanaged)

[StructLayout(LayoutKind.Sequential, Size=1), NativeCppClass,
DebugInfoInPDB, MiscellaneousBits(0x40), CLSCompliant(false)]
public struct UsesPragmaManaged
{
}

or this: (preceded by #pragma unmanaged)

[StructLayout(LayoutKind.Sequential, Size=1), MiscellaneousBits(0x40),
NativeCppClass, CLSCompliant(false), DebugInfoInPDB]
public struct UsesPragmaUnmanaged
{
}

Both classes contain an identical method (which is missing in
reflector),that when called runs 4 times faster in #pragma unmanaged.
What I don't understand is how the UsesPragmaManaged code is handled
by the CLR, and why it is running slower, when it seems to be
identical.

Much thanks,
HC
 
H

Henri.Chinasque

Hi guys,

thanks for your responses, a big help. I then got curious and had a
look in Reflector to see exactly what is happening with my classes
that are preceded by either pragma managed or unmanaged. The strange
thing is that they look identical, either:

(preceded by #pragma managed)

[StructLayout(LayoutKind.Sequential, Size=1), NativeCppClass,
DebugInfoInPDB, MiscellaneousBits(0x40), CLSCompliant(false)]
public struct UsesPragmaManaged
{
}

- or - (preceded by #pragma unmanaged)

[StructLayout(LayoutKind.Sequential, Size=1), MiscellaneousBits(0x40),
NativeCppClass, CLSCompliant(false), DebugInfoInPDB]
public struct UsesPragmaUnmanaged
{
}

Both classes contain an identical method (which is missing from both
in Reflector), that runs 4 times faster with the UsesPragmaUnmanaged
class. This is all fine and good, but I don't understand how the CLR
is handling the UsesPragmaManaged class - it doesn't look any
different...

Much thanks,
HC
 
M

Mark Salsbery [MVP]

Hi guys,

thanks for your responses, a big help. I then got curious and had a
look in Reflector to see exactly what is happening with my classes
that are preceded by either pragma managed or unmanaged. The strange
thing is that they look identical, either:

(preceded by #pragma managed)

[StructLayout(LayoutKind.Sequential, Size=1), NativeCppClass,
DebugInfoInPDB, MiscellaneousBits(0x40), CLSCompliant(false)]
public struct UsesPragmaManaged
{
}

- or - (preceded by #pragma unmanaged)

[StructLayout(LayoutKind.Sequential, Size=1), MiscellaneousBits(0x40),
NativeCppClass, CLSCompliant(false), DebugInfoInPDB]
public struct UsesPragmaUnmanaged
{
}

Both classes contain an identical method (which is missing from both
in Reflector), that runs 4 times faster with the UsesPragmaUnmanaged


4 times faster? What does this method do?

Mark
 
H

Henri.Chinasque

here it is:

int simple(int n[], int count) {
int x=1;
for(int i=0;i<count;i++) {
int y=i;
for(int i=0;i<count;i++) {
y+=n;
}
x*=y;
}
return x;
}

n has a length of 80000 (this is also the value of count) and is
populated with random numbers. Same array is used for every call.

I could understand if it runs faster in native, what I don't
understand is how "native" classes are handled by the CLR when they
are compiled managed, i.e. why is the managed version running so much
more slowly when the code from Reflector looks the same as the
unmanaged version?

thanks again,
HC

thanks for your responses, a big help. I then got curious and had a
look in Reflector to see exactly what is happening with my classes
that are preceded by either pragma managed or unmanaged. The strange
thing is that they look identical, either:
(preceded by #pragma managed)
[StructLayout(LayoutKind.Sequential, Size=1), NativeCppClass,
DebugInfoInPDB, MiscellaneousBits(0x40), CLSCompliant(false)]
public struct UsesPragmaManaged
{
}
- or - (preceded by #pragma unmanaged)
[StructLayout(LayoutKind.Sequential, Size=1), MiscellaneousBits(0x40),
NativeCppClass, CLSCompliant(false), DebugInfoInPDB]
public struct UsesPragmaUnmanaged
{
}
Both classes contain an identical method (which is missing from both
in Reflector), that runs 4 times faster with the UsesPragmaUnmanaged

4 times faster?  What does this method do?

Mark

--
Mark Salsbery
Microsoft MVP - Visual C++


class. This is all fine and good, but I don't understand how the CLR
is handling the UsesPragmaManaged class - it doesn't look any
different...
Much thanks,
HC

- Show quoted text -- Hide quoted text -

- Show quoted text -
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top