Native C ++ vs C ++ / CLI Speed

I have a question regarding C ++ / CLI vs Native C ++ speeds. I wrote a small test application, and I see very amazing results.

Unmanaged C ++ / CLI code seems to be significantly slower. Basically, I created two console applications. One standard win32 console application and CLR console application.

Here is the code I made for the test. I saved 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;
}

Managed Output Code:

Called
Time Elapsed: 9937
Number of random: 20000000

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

Called
Time Elapsed: 24516
Number of random: 20000000

Enter your own code:

Called
Time Elapsed: 2156
Number of random: 20000000

GrowBalance 90 . . -, , , 4,5 , ++/CLI. , . , , , ++, . .

Update: visual studio 2005 2008 . ++.

# 2: . ~ 5000

2005 . 1875 . , 2005 CLI. - , .

+3
1

, , - , ++ , , (.. , , ). "" Visual Studio .

. , , dBalance , dBalance > 22000.0 .

, ( 17 , 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

:

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

:

if (numberOfYears > 14) {
  numberOfRandom += (numberOfYears >= 17)? 3: numberOfYears - 14;
}
+3

Source: https://habr.com/ru/post/1772029/


All Articles