I was worried about C # speed when it comes to heavy computing, when you need to use the raw processor power.
I always thought that C ++ is much faster than C # when it comes to computing. So I did some quick tests. The first test calculates prime numbers <integer n, the second test calculates some pandigital numbers. The idea for the second test comes from here: Pandigital Numbers
C # calculation:
using System; using System.Diagnostics; class Program { static int primes(int n) { uint i, j; int countprimes = 0; for (i = 1; i <= n; i++) { bool isprime = true; for (j = 2; j <= Math.Sqrt(i); j++) if ((i % j) == 0) { isprime = false; break; } if (isprime) countprimes++; } return countprimes; } static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); Stopwatch sw = new Stopwatch(); sw.Start(); int res = primes(n); sw.Stop(); Console.WriteLine("I found {0} prime numbers between 0 and {1} in {2} msecs.", res, n, sw.ElapsedMilliseconds); Console.ReadKey(); } }
C ++ option:
#include <iostream> #include <ctime> #include <cmath> int primes(unsigned long n) { unsigned long i, j; int countprimes = 0; for(i = 1; i <= n; i++) { int isprime = 1; for(j = 2; j < sqrt((float)i); j++) if(!(i%j)) { isprime = 0; break; } countprimes+= isprime; } return countprimes; } int main() { int n, res; cin>>n; unsigned int start = clock(); res = primes(n); int tprime = clock() - start; cout<<"\nI found "<<res<<" prime numbers between 1 and "<<n<<" in "<<tprime<<" msecs."; return 0; }
When I ran the test, trying to find primes <less than 100,000, the C # variant completed in 0.409 seconds and the C ++ variant in 0.614 seconds. When I launched them in 1,000,000 C #, I finished in 6.039 seconds and C ++ in about 12.987 seconds.
Pandigital test in C #:
using System; using System.Diagnostics; class Program { static bool IsPandigital(int n) { int digits = 0; int count = 0; int tmp; for (; n > 0; n /= 10, ++count) { if ((tmp = digits) == (digits |= 1 << (n - ((n / 10) * 10) - 1))) return false; } return digits == (1 << count) - 1; } static void Main() { int pans = 0; Stopwatch sw = new Stopwatch(); sw.Start(); for (int i = 1; i <= 123456789; i++) { if (IsPandigital(i)) { pans++; } } sw.Stop(); Console.WriteLine("{0}pcs, {1}ms", pans, sw.ElapsedMilliseconds); Console.ReadKey(); } }
Pandigital test in C ++:
#include <iostream> #include <ctime> using namespace std; int IsPandigital(int n) { int digits = 0; int count = 0; int tmp; for (; n > 0; n /= 10, ++count) { if ((tmp = digits) == (digits |= 1 << (n - ((n / 10) * 10) - 1))) return 0; } return digits == (1 << count) - 1; } int main() { int pans = 0; unsigned int start = clock(); for (int i = 1; i <= 123456789; i++) { if (IsPandigital(i)) { pans++; } } int ptime = clock() - start; cout<<"\nPans:"<<pans<<" time:"<<ptime; return 0; }
The C # variant works for 29906 seconds and C ++ after about 36.298 seconds.
I did not touch any compilers, and both C # and C ++ programs were compiled with debugging options. Before I tried to run the test, I was worried that C # was significantly behind C ++, but now it seems that C # has a pretty big difference in speed.
Can someone explain this? C # is jitted and C ++ compiled as a native, so itβs normal that C ++ will be faster than the C # variant.
Thanks for answers!
I changed all the tests for the Release configuration.
First test (prime numbers)
C # (digits <100.0000): 0.189 seconds C ++ (digits <100.0000): 0.036 seconds
C # (nummbers <1,000,000): 5,300 seconds C ++ (nummbers <1,000,000): 1,166 seconds
Second test (pandigital numbers):
C #: 21.224 seconds C ++: 4.104 seconds
So, everything has changed, now C ++ is much faster. My mistake is that I checked the test for Debug configuration. Can I see a speed improvement if I run C # executables via ngen?
The reason I tried to compare C # and C ++ is because I know some of the basics of both, and I wanted to learn the API that works with the GUI. I thought WPF was good, so given that I am focused on the desktop, I wanted to know if C # can provide sufficient speed and performance when it comes to using the pure processor power to calculate various calculations (file archivers, cryptography, codecs, etc.), but it seems that C # cannot keep up with C ++ when it comes to speed.
So, I assume that I will stay with this question forever. There is a tough question about WPF, Win32, MFC , and I will find a suitable API.