Amazing C/C++ and C# and Python performance test
-
I tried to do a basic calculation for testing the performance of different programming languages: C/C++: (***3.05 s***)
int main()
{
int start = clock();
double result;
//<...>
for (size_t i = 1; i < 100000000; i++)
{
result = sin((i * 25) / i * i);
}
//<...>
int end = clock();//Now check what amount of ticks we have now.
//To get the time, just subtract start from end, and divide by CLOCKS_PER_SEC.
std::cout << "it took " << end - start << "ticks std::endl;
}C#: (***2.452 s***)
using System.Diagnostics;
class TestClass
{
static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
double result;sw.Start(); { for (int i = 1; i < 100000000; i++) { result = Math.Sin((i \* 25)/i \* i); } } sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds.ToString()); }
}
And the same calculation in Python took 22.14 s to complete Why is C# faster than C++?:confused::confused::confused:
-
I tried to do a basic calculation for testing the performance of different programming languages: C/C++: (***3.05 s***)
int main()
{
int start = clock();
double result;
//<...>
for (size_t i = 1; i < 100000000; i++)
{
result = sin((i * 25) / i * i);
}
//<...>
int end = clock();//Now check what amount of ticks we have now.
//To get the time, just subtract start from end, and divide by CLOCKS_PER_SEC.
std::cout << "it took " << end - start << "ticks std::endl;
}C#: (***2.452 s***)
using System.Diagnostics;
class TestClass
{
static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
double result;sw.Start(); { for (int i = 1; i < 100000000; i++) { result = Math.Sin((i \* 25)/i \* i); } } sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds.ToString()); }
}
And the same calculation in Python took 22.14 s to complete Why is C# faster than C++?:confused::confused::confused:
I'd expect all "intermediate" calculations in C# to be performed as "int", since you have not supplied a single float or double on the right of the equation. And you neglected to display / verify "result".
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
-
I tried to do a basic calculation for testing the performance of different programming languages: C/C++: (***3.05 s***)
int main()
{
int start = clock();
double result;
//<...>
for (size_t i = 1; i < 100000000; i++)
{
result = sin((i * 25) / i * i);
}
//<...>
int end = clock();//Now check what amount of ticks we have now.
//To get the time, just subtract start from end, and divide by CLOCKS_PER_SEC.
std::cout << "it took " << end - start << "ticks std::endl;
}C#: (***2.452 s***)
using System.Diagnostics;
class TestClass
{
static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
double result;sw.Start(); { for (int i = 1; i < 100000000; i++) { result = Math.Sin((i \* 25)/i \* i); } } sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds.ToString()); }
}
And the same calculation in Python took 22.14 s to complete Why is C# faster than C++?:confused::confused::confused:
Note that `Math.Sin` is implemented in native code, see the [Reference Source](https://referencesource.microsoft.com/#mscorlib/system/math.cs,84180d208256f9c3) (not for the implementation, but for the fact that there's no implementation there). So this isn't really a benchmark of C# vs C++, but one sine written in C++ vs another sine written in C++.
-
I tried to do a basic calculation for testing the performance of different programming languages: C/C++: (***3.05 s***)
int main()
{
int start = clock();
double result;
//<...>
for (size_t i = 1; i < 100000000; i++)
{
result = sin((i * 25) / i * i);
}
//<...>
int end = clock();//Now check what amount of ticks we have now.
//To get the time, just subtract start from end, and divide by CLOCKS_PER_SEC.
std::cout << "it took " << end - start << "ticks std::endl;
}C#: (***2.452 s***)
using System.Diagnostics;
class TestClass
{
static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
double result;sw.Start(); { for (int i = 1; i < 100000000; i++) { result = Math.Sin((i \* 25)/i \* i); } } sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds.ToString()); }
}
And the same calculation in Python took 22.14 s to complete Why is C# faster than C++?:confused::confused::confused:
Both of the previous answers are correct. On my machine, with VS2019, this code
static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
double result=0.0;sw.Start(); { for (int i = 1; i < 100000000; i++) { result += Math.Sin((i \* 25.0) / i \* i); } } sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds.ToString()); Console.WriteLine(string.Format("{0}",result)); }
produces
4739
-7,98150964297362While the following
int main()
{
int start = clock();
double result = 0.0;
for (size_t i = 1; i < 100000000; i++)
{
result += sin((i * 25.0) / i * i);
}
int end = clock();//Now check what amount of ticks we have now.
//To get the time, just subtract start from end, and divide by CLOCKS_PER_SEC.
std::cout << (1000.0 * (end - start) / CLOCKS_PER_SEC) << "\n";
std::cout << result << "\n";
}outputs
4153
-7.98151"In testa che avete, Signor di Ceprano?" -- Rigoletto