Specific instances where native code runs faster

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Henri.Chinasque@googlemail.com

    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.

  • David Lowndes

    #2
    Re: Specific instances where native code runs faster

    >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

    Comment

    • Henri.Chinasque@googlemail.com

      #3
      Re: Specific instances where native code runs faster

      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

      Comment

      • David Lowndes

        #4
        Re: Specific instances where native code runs faster

        >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 "Performanc e 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

        Comment

        • Ben Voigt [C++ MVP]

          #5
          Re: Specific instances where native code runs faster

          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 "Performanc e 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

          Comment

          • Henri.Chinasque@googlemail.com

            #6
            Re: Specific instances where native code runs faster

            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(La youtKind.Sequen tial, Size=1), NativeCppClass,
            DebugInfoInPDB, MiscellaneousBi ts(0x40), CLSCompliant(fa lse)]
            public struct UsesPragmaManag ed
            {
            }

            or this: (preceded by #pragma unmanaged)

            [StructLayout(La youtKind.Sequen tial, Size=1), MiscellaneousBi ts(0x40),
            NativeCppClass, CLSCompliant(fa lse), DebugInfoInPDB]
            public struct UsesPragmaUnman aged
            {
            }

            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 UsesPragmaManag ed 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.no spamwrote:
            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 "Performanc e 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 -

            Comment

            • Henri.Chinasque@googlemail.com

              #7
              Re: Specific instances where native code runs faster

              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(La youtKind.Sequen tial, Size=1), NativeCppClass,
              DebugInfoInPDB, MiscellaneousBi ts(0x40), CLSCompliant(fa lse)]
              public struct UsesPragmaManag ed
              {
              }

              - or - (preceded by #pragma unmanaged)

              [StructLayout(La youtKind.Sequen tial, Size=1), MiscellaneousBi ts(0x40),
              NativeCppClass, CLSCompliant(fa lse), DebugInfoInPDB]
              public struct UsesPragmaUnman aged
              {
              }

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

              Much thanks,
              HC



              On Jul 23, 3:31 pm, "Ben Voigt [C++ MVP]" <r...@nospam.no spamwrote:
              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 "Performanc e 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 -

              Comment

              • Mark Salsbery [MVP]

                #8
                Re: Specific instances where native code runs faster

                <Henri.Chinasqu e@googlemail.co mwrote in message
                news:d96c0bc8-3916-45d6-bc0e-6e7e4c4a0b18@j3 3g2000pri.googl egroups.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(La youtKind.Sequen tial, Size=1), NativeCppClass,
                DebugInfoInPDB, MiscellaneousBi ts(0x40), CLSCompliant(fa lse)]
                public struct UsesPragmaManag ed
                {
                }
                >
                - or - (preceded by #pragma unmanaged)
                >
                [StructLayout(La youtKind.Sequen tial, Size=1), MiscellaneousBi ts(0x40),
                NativeCppClass, CLSCompliant(fa lse), DebugInfoInPDB]
                public struct UsesPragmaUnman aged
                {
                }
                >
                Both classes contain an identical method (which is missing from both
                in Reflector), that runs 4 times faster with the UsesPragmaUnman aged

                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 UsesPragmaManag ed class - it doesn't look any
                different...
                >
                Much thanks,
                HC
                >
                >
                >
                On Jul 23, 3:31 pm, "Ben Voigt [C++ MVP]" <r...@nospam.no spamwrote:
                >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 "Performanc e 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 -
                >

                Comment

                • Henri.Chinasque@googlemail.com

                  #9
                  Re: Specific instances where native code runs faster

                  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.nosp amwrote:
                  <Henri.Chinas.. .@googlemail.co mwrote in message
                  >
                  news:d96c0bc8-3916-45d6-bc0e-6e7e4c4a0b18@j3 3g2000pri.googl egroups.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(La youtKind.Sequen tial, Size=1), NativeCppClass,
                  DebugInfoInPDB, MiscellaneousBi ts(0x40), CLSCompliant(fa lse)]
                  public struct UsesPragmaManag ed
                  {
                  }
                  >
                  - or - (preceded by #pragma unmanaged)
                  >
                  [StructLayout(La youtKind.Sequen tial, Size=1), MiscellaneousBi ts(0x40),
                  NativeCppClass, CLSCompliant(fa lse), DebugInfoInPDB]
                  public struct UsesPragmaUnman aged
                  {
                  }
                  >
                  Both classes contain an identical method (which is missing from both
                  in Reflector), that runs 4 times faster with the UsesPragmaUnman aged
                  >
                  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 UsesPragmaManag ed class - it doesn't look any
                  different...
                  >
                  Much thanks,
                  HC
                  >
                  On Jul 23, 3:31 pm, "Ben Voigt [C++ MVP]" <r...@nospam.no spamwrote:
                  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 "Performanc e 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 -

                  Comment

                  Working...