Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# vs C - Big performance difference

Tags:

performance

c

c#

I'm finding massive performance differences between similar code in C and C#.

The C code is:

#include <stdio.h> #include <time.h> #include <math.h>  main() {     int i;     double root;          clock_t start = clock();     for (i = 0 ; i <= 100000000; i++){         root = sqrt(i);     }     printf("Time elapsed: %f\n", ((double)clock() - start) / CLOCKS_PER_SEC);     } 

And the C# (console app) is:

using System; using System.Collections.Generic; using System.Text;  namespace ConsoleApplication2 {     class Program     {         static void Main(string[] args)         {             DateTime startTime = DateTime.Now;             double root;             for (int i = 0; i <= 100000000; i++)             {                 root = Math.Sqrt(i);             }             TimeSpan runTime = DateTime.Now - startTime;             Console.WriteLine("Time elapsed: " + Convert.ToString(runTime.TotalMilliseconds/1000));         }     } } 

With the above code, the C# completes in 0.328125 seconds (release version) and the C takes 11.14 seconds to run.

The C is being compiled to a Windows executable using mingw.

I've always been under the assumption that C/C++ were faster or at least comparable to C#.net. What exactly is causing the C code to run over 30 times slower?

EDIT: It does appear that the C# optimizer was removing the root as it wasn't being used. I changed the root assignment to root += and printed out the total at the end. I've also compiled the C using cl.exe with the /O2 flag set for max speed.

The results are now: 3.75 seconds for the C 2.61 seconds for the C#

The C is still taking longer, but this is acceptable.

like image 636
John Avatar asked Mar 26 '09 16:03

John


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is C full form?

History: The name C is derived from an earlier programming language called BCPL (Basic Combined Programming Language). BCPL had another language based on it called B: the first letter in BCPL.


2 Answers

You must be comparing debug builds. I just compiled your C code, and got

Time elapsed: 0.000000 

If you don't enable optimizations, any benchmarking you do is completely worthless. (And if you do enable optimizations, the loop gets optimized away. So your benchmarking code is flawed too. You need to force it to run the loop, usually by summing up the result or similar, and printing it out at the end)

It seems that what you're measuring is basically "which compiler inserts the most debugging overhead". And turns out the answer is C. But that doesn't tell us which program is fastest. Because when you want speed, you enable optimizations.

By the way, you'll save yourself a lot of headaches in the long run if you abandon any notion of languages being "faster" than each others. C# no more has a speed than English does.

There are certain things in the C language that would be efficient even in a naive non-optimizing compiler, and there are others that relies heavily on a compiler to optimize everything away. And of course, the same goes for C# or any other language.

The execution speed is determined by:

  • the platform you're running on (OS, hardware, other software running on the system)
  • the compiler
  • your source code

A good C# compiler will yield efficient code. A bad C compiler will generate slow code. What about a C compiler which generated C# code, which you could then run through a C# compiler? How fast would that run? Languages don't have a speed. Your code does.

like image 180
jalf Avatar answered Nov 03 '22 13:11

jalf


I'll keep it brief, it is already marked answered. C# has the great advantage of having a well defined floating point model. That just happens to match the native operation mode of the FPU and SSE instruction set on x86 and x64 processors. No coincidence there. The JITter compiles Math.Sqrt() to a few inline instructions.

Native C/C++ is saddled with years of backwards compatibility. The /fp:precise, /fp:fast and /fp:strict compile options are the most visible. Accordingly, it must call a CRT function that implements sqrt() and checks the selected floating point options to adjust the result. That's slow.

like image 29
Hans Passant Avatar answered Nov 03 '22 15:11

Hans Passant