473,397 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Specific instances where native code runs faster

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.

Jul 22 '08 #1
8 2396
>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
Jul 22 '08 #2
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

On Jul 22, 5:27*pm, David Lowndes <Dav...@example.invalidwrote:
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
Jul 23 '08 #3
>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
Jul 23 '08 #4
David Lowndes wrote:
>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.
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.
>
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

Jul 23 '08 #5
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

On Jul 23, 3:31*pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
David Lowndes wrote:
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.

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..


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- Hide quoted text -

- Show quoted text -
Jul 24 '08 #6
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

On Jul 23, 3:31*pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
David Lowndes wrote:
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.

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..


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- Hide quoted text -

- Show quoted text -
Jul 24 '08 #7
<He*************@googlemail.comwrote in message
news:d9**********************************@j33g2000 pri.googlegroups.com...
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

--
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

On Jul 23, 3:31 pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
>David Lowndes wrote:
>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.

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.


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- Hide quoted text -

- Show quoted text -
Jul 24 '08 #8
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[i];
}
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

On Jul 24, 11:39*am, "Mark Salsbery [MVP]"
<MarkSalsbery[MVP]@newsgroup.nospamwrote:
<Henri.Chinas...@googlemail.comwrote in message

news:d9**********************************@j33g2000 pri.googlegroups.com...


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

--
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
On Jul 23, 3:31 pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
David Lowndes wrote:
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.
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 duringa
garbage collection. Marshalling COM objects will probably be the worst..
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- Hide quoted text -
- Show quoted text -- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -
Jul 24 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Dan | last post by:
Hi Gurus I got a very basic question to ask: When a .NET exe (MSIL) is first run, the JIT-compiler will converts the IL into native codes so that it can executes on the current machine. my...
2
by: Balaji | last post by:
Hi folks, I heard the nGen is the tool to generate the native code so that the response WIll be faster? what is my doubt is? how to specify the location of the native code of particular...
7
by: Daniel Dünker | last post by:
Hello. I was screwing around a bit with the exe-files produced by .Net Compilers and trying to understand how they work... so i ended up at the 6 Byte stub, which calls the _CorExeMain in...
8
by: quortex | last post by:
Hi all, I have a native class which has a single instance controlled via the singleton pattern. I need to call this from both native C++ and from mixed mode visual studio 2005 c++ CLI. At...
6
by: per9000 | last post by:
An interesting/annoying problem. I created a small example to provoke an exception I keep getting. Basically I have a C-struct (Container) with a function-pointer in it. I perform repeated calls...
10
by: =?Utf-8?B?TWF4IDFlNg==?= | last post by:
I have an application form named Form1.h. The code for this form is a namespace (MyNamespace) with two classes in it (Form1 and MyClass). Form1 has windows designer code and some button event...
3
by: =?Utf-8?B?VG9kZA==?= | last post by:
What is the memory footprint of static methods of a windows app running on a server when the server spins up multiple instances of the application? In my envirionment, we have a Citrix server...
4
by: Marcus | last post by:
Hello, Whenever the PHP documentation references the optional link_identifier parameter in many mysql related functions (such as mysql_query, mysql_insert_id, etc.), it says: "The MySQL...
19
by: Zytan | last post by:
I want multiple instances of the same .exe to run and share the same data. I know they all can access the same file at the same time, no problem, but I'd like to have this data in RAM, which they...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.