Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3353112
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:06:43+00:00 2026-05-18T02:06:43+00:00

I have a question regarding C++/CLI vs Native C++ speeds. I have written a

  • 0

I have a question regarding C++/CLI vs Native C++ speeds. I have written a little test application and I am seeing very surprising results.

It seems that the unmanaged C++/CLI code is significantly slower. Basically I created two console apps. One a standard win32 console app and a CLR console app

Here is the code I did for a test. I kept the code exactly the same in all versions of the test.

const int NumberOfTests = 10000000;
void GrowBalance(int numberOfYears)
{
 std::cout<<"Called"<<std::endl;

 DWORD startTime = GetTickCount();

 int numberOfRandom = 0;
 for(int i = 0; i < NumberOfTests; i++)
 {
  double dBalance = 10000.0;
  for(int year = 0; year < numberOfYears; year++)
  { 
   dBalance *= 1.05;
   if(dBalance > 20000.00 && dBalance < 22000.00)
   {
    numberOfRandom++;
   }//if
  }//for
 }//for

 DWORD endTime = GetTickCount();

 std::cout<<"Time Elapsed: "<<endTime - startTime<<std::endl;
 std::cout<<"Number of random: "<<numberOfRandom<<std::endl;
}

Output managed code:

Called
Time Elapsed: 9937
Number of random: 20000000

Output managed code with pragma managed(push, off):

Called
Time Elapsed: 24516
Number of random: 20000000

Output native code:

Called
Time Elapsed: 2156
Number of random: 20000000

In the mains just calling GrowBalance with 90 years specified. Pretty basic test. Is there something I am doing wrong or am I really looking at code that is going to be 4.5 times slower by using C++/CLI. And I also don’t understand the case of turning managed code off. Everything I have read said this would compile the code to native C++ but it is insanely slower. Any help with this would be very much appreciated.

Update:
I just ran this test in visual studio 2005 instead of 2008. Native C++ performance is matched.

Update #2:
I just put my test code into a class instead of a single function and am getting much better results. Now the mixed code is preforming at an average run time of ~5000ms

But in 2005 I am seeing much much faster results. Average run time of about ~1875ms. Maybe I will just stick to 2005 for my CLI development. Unless someone has a reason why this could be occurring.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T02:06:44+00:00Added an answer on May 18, 2026 at 2:06 am

    One thing you may be running into is that for native C++, optimizations are controlled by command-line arguments to the compiler, but for managed code, optimizations are controlled by how you start the application (i.e. if you launch in debugger, many optimizations are disabled even if you did an optimized build). You shouldn’t be running performance tests “in” Visual Studio at all.

    The native compiler also has a LOT of extra optimizations. It might even be smart enough to figure out that dBalance is strictly increasing, and continuing the inner for loop once dBalance > 22000.0 has no observable side effects.

    What happens in all three cases if you change that inner for loop like this (it will only do 17 iterations, as long as numberOfYears >= 17)?

      double dBalance = 10000.0;
      for(int year = 0; year < numberOfYears && dBalance < 22000.0; year++)
      { 
       dBalance *= 1.05;
       if(dBalance > 20000.0)
       {
        numberOfRandom++;
       }//if
      }//for
    

    How about:

    if (numberOfYears > 14) {
      double dBalance = 19799.315994393973883056640625;
      for(int year = 14; year < numberOfYears && dBalance < 22000.0; year++)
      { 
       dBalance *= 1.05;
       numberOfRandom++;
      }//for
    }
    

    And how about:

    if (numberOfYears > 14) {
      numberOfRandom += (numberOfYears >= 17)? 3: numberOfYears - 14;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question regarding handling errors in a J2EE application. Our current application
I have question regarding associations in Ruby on Rails. In the application there are
I have a question regarding the two additional columns (timeCreated, timeLastUpdated) for each record
I have a question regarding an update function I created... CREATE OR REPLACE FUNCTION
I'm writing a small webapp in Grails and I have the following question regarding
Following on from my recent question regarding parsing XML files in Java I have
I have a question about best practices regarding how one should approach storing complex
I have a simple question and wish to hear others' experiences regarding which is
I have question regarding the SQLAlchemy. How can I add into my mapped class
I have question regarding the use of function parameters. In the past I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.