-4

I was solving a simple coding challenge on an online website. i have written the code in C, C++, and C# for the same problem. Once you solve the problem the website gives some statistics about the correctness and performance. here is what something struck my eye. the c# code took 0.06 seconds to execute and give me the result where as C took 0.006 seconds and c++ took 0.009 seconds.

My question, why does c# code run 10 times slower? or for that matter of fact c/c++ run 10 times faster?

links to the website below.

C code
C++ Code
C# Code

i am assuming that same test cases have been on all the three code blocks.

AMS
  • 550
  • 6
  • 22
  • You might find use in reading the answers to http://stackoverflow.com/q/686483/79061 – Sebastian Paaske Tørholm Jun 12 '15 at 15:22
  • What do you expect as answer? A general rule like "C++ is x times faster/slower" doesn´t exist. However, C++ is "often" faster than languages like C# and Java (if the codes are well written for every compared language. Writing slow programs is possible in every language) – deviantfan Jun 12 '15 at 15:27
  • 1
    Providing code as external links is a bad idea. If the links go dead then this question becomes useless. – Daniel Kelley Jun 12 '15 at 15:28
  • @deviantfan Dont get me wrong here. i am not asking why it is X times faster. my intention was to ask possible reasons why code runs slower. – AMS Jun 12 '15 at 15:29
  • I think you need to perform more experiments (and I don't mean via code-running-websites) to back your observation/conclusion that _"why does c# code run 10 times slower [than c]?"_. Also, did you run the c# app again immediately after? I bet performance would have been different due to JIT caching –  Jun 12 '15 at 15:31
  • Do you really know what the time represents? Is it time from source to answer or single run of exe or something totally different? Make *your own measurements* to make sure you compare apple to apples. You'll need to be careful to exclude JIT portion of the time if you are looking for pure code speed. (There are many good answers on how to properly measure execution time for managed code). – Alexei Levenkov Jun 12 '15 at 15:41

2 Answers2

2

C# is compiled into Common Intermediate Language. This is then run Just In Time (JIT). Essentially, the code is compiled as it is needed. C and C++ compile into the computers native assembly language. This means that nothing additional needs to happen while it is running. While this is a broad overview, I would implore you to look into JIT and CIL, as this will help you understand on a more in depth level. You can view them here: Common Intermediate Language, Just In Time

David
  • 236
  • 1
  • 12
  • 1
    Generally speaking, virtual machines / execution environments often have a startup overhead / latency. This is a worse problem with the Java VM than with the CLR. It's entirely possible that the C# code actually runs just as fast as the equivalent C++ code (or at least that execution times are in the same order of magnitude)... only with C#, it might take longer to get to the point where the code can be executed by the CPU (start up the VM, load and interpret IL metadata, JIT-compile methods). – stakx - no longer contributing Jun 12 '15 at 15:35
  • I upvoted and edited my comment, he was correct initially. I had completely derped out on part of it. @MickyDuncan – David Jun 12 '15 at 15:35
  • Additional point - site may include JIT time into the measurement completely skewing the results... – Alexei Levenkov Jun 12 '15 at 15:43
0

There is no strict reason why a bytecode based language like C# or Java that has a JIT cannot be as fast as C++ code. However C++ code used to be significantly faster for a long time, and also today still is in many cases. This is mainly due to the more advanced JIT optimizations being complicated to implement, and the really cool ones are only arriving just now.

So C++ is faster, in many cases. But this is only part of the answer. The cases where C++ is actually faster, are highly optimized programs, where expert programmers thoroughly optimized the hell out of the code. This is not only very time consuming (and thus expensive), but also commonly leads to errors due to over-optimizations.

Remember, a quick test like yours doesn't mean much. c++/c are going to have near identical run-timesto c# in many, many situations except for some ealmost every situation you are likely to encounter.

Olivier Poulin
  • 1,778
  • 8
  • 15