I browsed through many topics suggesting using c# instead of c++/cli to create winforms. Among the reasons given, I didn't see anyone mention something about the speed so I insisted on using c++/cli for a long time.
Lately I tried a simple calculation loop to compare the speed of c++/cli and c#. It took the same time on a debug build. But the release build of the c# project got a huge optimization( 1/4 calculation time) while the c++/cli got no optimization at all. Is this normal or did I miss something?
I built the solution for C# in VS2022 Community and for c++/cli in both VS2022(.net version 4.7.2) and VS2010 Express(.net version 4.0).
I copy the code for c++/cli. The c# project did the same calculation.
#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <windows.h>
unsigned int cal1(double &a1)
{
time_t t0 = time(0);
SYSTEMTIME ts0,ts1;
GetLocalTime(&ts0);
for(unsigned int idx1=0;idx1<0xFFFFFFFF;++idx1) a1+=0.328;
GetLocalTime(&ts1);
unsigned int t1 = (time(0)-t0)*1000 + ts1.wMilliseconds-ts0.wMilliseconds;
return t1;
}
ref class Form0 : System::Windows::Forms::Form
{
public:
System::Windows::Forms::Button^ bt1;
System::Windows::Forms::Label^ lbl1;
Form0(void)
{
this->SuspendLayout();
this->bt1 = gcnew System::Windows::Forms::Button();
this->bt1->Location = System::Drawing::Point(95, 18);
this->bt1->Size = System::Drawing::Size(50, 25);
this->bt1->Text = "button1";
this->bt1->Click += gcnew System::EventHandler(this, &Form0::bt1_Click);
this->Controls->Add(this->bt1);
this->lbl1 = gcnew System::Windows::Forms::Label();
this->lbl1->Location = System::Drawing::Point(72, 62);
this->lbl1->Size = System::Drawing::Size(60, 20);
this->Controls->Add(this->lbl1);
}
System::Void bt1_Click(System::Object^ sender, System::EventArgs^ e)
{
double a1 = 1;
unsigned int t1;
t1=cal1(a1);
this->lbl1->Text = t1.ToString();
}
};
[System::STAThreadAttribute]
int main()
{
Form0^ F0 = gcnew Form0();
F0->ShowDialog();
return 0;
}