Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / July 2008

Tip: Looking for answers? Try searching our database.

Specific instances where native code runs faster

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Henri.Chinasque@googlemail.com - 22 Jul 2008 21:29 GMT
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.
David Lowndes - 23 Jul 2008 00:27 GMT
>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
Henri.Chinasque@googlemail.com - 23 Jul 2008 20:25 GMT
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

> >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
[quoted text clipped - 12 lines]
>
> Dave
David Lowndes - 23 Jul 2008 21:29 GMT
>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
Ben Voigt [C++ MVP] - 23 Jul 2008 22:30 GMT
>> What I'm now struggling to understand is if I could incur performance
>> penalties by calling an unmanaged function (converted to unmanaged
[quoted text clipped - 4 lines]
> 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
Henri.Chinasque@googlemail.com - 24 Jul 2008 18:00 GMT
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

> >> What I'm now struggling to understand is if I could incur performance
> >> penalties by calling an unmanaged function (converted to unmanaged
[quoted text clipped - 17 lines]
>
> - Show quoted text -
Henri.Chinasque@googlemail.com - 24 Jul 2008 18:13 GMT
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

> >> What I'm now struggling to understand is if I could incur performance
> >> penalties by calling an unmanaged function (converted to unmanaged
[quoted text clipped - 17 lines]
>
> - Show quoted text -
Mark Salsbery [MVP] - 24 Jul 2008 18:39 GMT
> Hi guys,
>
[quoted text clipped - 21 lines]
> 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

Signature

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
[quoted text clipped - 25 lines]
>>
>> - Show quoted text -
Henri.Chinasque@googlemail.com - 24 Jul 2008 20:20 GMT
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.nospam> wrote:
> <Henri.Chinas...@googlemail.com> wrote in message
>
[quoted text clipped - 67 lines]
>
> - Show quoted text -

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.