Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Everyone says c++ is faster than c#, why?

Everyone says c++ is faster than c#, why?

Scheduled Pinned Locked Moved The Lounge
csharpc++algorithmsquestion
64 Posts 46 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Tomaz Stih 0

    Others have already nailed it down to C++ streams. Besides that I remember reading somewhere that the execution on modern highly parallelized systems also highly depends on the structure of the code. For example: modern systems would read several machine instructions ahead and analyze them while executing the current one. So if your code would have multiple jump instructions which would prevent effective pipelining this would probably run slower. Note that I'm not 100% practically sure of what I just wrote. It's theoretical. :)

    Z Offline
    Z Offline
    ZurdoDev
    wrote on last edited by
    #14

    Tomaž Štih wrote:

    I'm not 100% practically sure of what I just wrote.

    I'd suggest that as a signature for some of our regular Lounge participants. I won't name names. :^)

    There are only 10 types of people in the world, those who understand binary and those who don't.

    N 1 Reply Last reply
    0
    • C Chris Maunder

      We all know that VB is faster than them both.

      cheers Chris Maunder

      N Offline
      N Offline
      Nagy Vilmos
      wrote on last edited by
      #15

      Shirley you mean VB on Rails!

      veni bibi saltavi

      1 Reply Last reply
      0
      • P phil o

        Have you read the Lounge's guidelines which says "No programming questions in the Lounge"? You have to use appropriate forum for your questions; Quick Ansers section would be appropriate in this case.

        You always obtain more by being rather polite and armed than polite only.

        M Offline
        M Offline
        Maximilien
        wrote on last edited by
        #16

        Are we that picky now that we cannot even talk about programming language performance in the Lounge ? :confused: If that is the case, then we should rename the site LoungeProject.com ...

        I'd rather be phishing!

        1 Reply Last reply
        0
        • P phil o

          Shall we cut that in half and say it's a programming discussion, or a technical question, then? :) I'm not sure that fits here, anyway.

          You always obtain more by being rather polite and armed than polite only.

          R Offline
          R Offline
          realJSOP
          wrote on last edited by
          #17

          You sound like a liberal. On and you're wrong. It's simply a technical discussion.

          ".45 ACP - because shooting twice is just silly" - JSOP, 2010
          -----
          You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
          -----
          When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

          Y 1 Reply Last reply
          0
          • A amagitech

            I used same algorithm for c# and c++. I have read c++ is faster than c# but my tests shows c# is faster than c++. What's wrong? My codes like this. c++ is 36 seconds

            #include
            #include

            using namespace std;

            int main(){
            const clock_t beginTime = clock();
            for (int i = 2; i < 2000000; i++)
            {
            bool isPrime = true;
            for (int j = 2; j*j <= i; j++)
            {
            if (i%j == 0){
            isPrime = false;
            break;
            }
            }
            if (isPrime)
            cout << i << endl;
            }
            cout << float(clock() - beginTime) / CLOCKS_PER_SEC;
            }

            c# is 9 seconds

            using System;

            namespace Console01
            {
            class Program
            {

                static void Main(string\[\] args)
                {
                    DateTime beginTime = DateTime.Now;
                    for (int i = 2; i <= 2000000; i++)
                    {
                        bool isPrime = true;
                        for (int j = 2; j \* j < i; j++)
                        {
                            if (i % j == 0)
                            {
                                isPrime = false;
                                break;
                            }
                        }
                        if (isPrime)
                            Console.WriteLine(i);
                    }
                    TimeSpan ts = DateTime.Now - beginTime;
                    Console.WriteLine(ts.Seconds);
                }
            
            }
            

            }

            V Offline
            V Offline
            Vark111
            wrote on last edited by
            #18

            Not anyone I know says C++ *is* faster than C#. Most of the people I hear talking about it say C++ *can be* faster than C#. It's much easier to play memory arithmetic tricks in C++ than C#. You can do it in C# using Memory Pins and Unsafe blocks, but it's a lot more code to write just to get to a point where you can start doing the actual work of manipulating your values. Also, trivial examples like that won't show you the biggest culprit of performance issues in any .net language: the GC. Once the GC starts grinding away, you can kiss anything that resembles good performance goodbye. You can of course mitigate this by using proper patterns and such, but that's a whole lot of mental overhead that the C++ programmer doesn't need to deal with.

            1 Reply Last reply
            0
            • C Chris Maunder

              We all know that VB is faster than them both.

              cheers Chris Maunder

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #19

              Chris Maunder wrote:

              We all know that VB is faster than them both.

              But not as fast as Coopers.

              Michael Martin Australia "I controlled my laughter and simple said "No,I am very busy,so I can't write any code for you". The moment they heard this all the smiling face turned into a sad looking face and one of them farted. So I had to leave the place as soon as possible." - Mr.Prakash One Fine Saturday. 24/04/2004

              C 1 Reply Last reply
              0
              • C Chris Maunder

                We all know that VB is faster than them both.

                cheers Chris Maunder

                D Offline
                D Offline
                Dan Neely
                wrote on last edited by
                #20

                Chris Maunder wrote:

                We all know that VB is faster than them both.

                ... I assume this is after you accelerated it out the muzzle of a cannon.

                Did you ever see history portrayed as an old man with a wise brow and pulseless heart, waging all things in the balance of reason? Is not rather the genius of history like an eternal, imploring maiden, full of fire, with a burning heart and flaming soul, humanly warm and humanly beautiful? --Zachris Topelius Training a telescope on one’s own belly button will only reveal lint. You like that? You go right on staring at it. I prefer looking at galaxies. -- Sarah Hoyt

                1 Reply Last reply
                0
                • L Lost User

                  Chris Maunder wrote:

                  We all know that VB is faster than them both.

                  But not as fast as Coopers.

                  Michael Martin Australia "I controlled my laughter and simple said "No,I am very busy,so I can't write any code for you". The moment they heard this all the smiling face turned into a sad looking face and one of them farted. So I had to leave the place as soon as possible." - Mr.Prakash One Fine Saturday. 24/04/2004

                  C Offline
                  C Offline
                  Chris Maunder
                  wrote on last edited by
                  #21

                  I had one of these[^] the other day (I was driving - needed something less than full strength) and all I can say is: What were they thinking? X|

                  cheers Chris Maunder

                  1 Reply Last reply
                  0
                  • Z ZurdoDev

                    Tomaž Štih wrote:

                    I'm not 100% practically sure of what I just wrote.

                    I'd suggest that as a signature for some of our regular Lounge participants. I won't name names. :^)

                    There are only 10 types of people in the world, those who understand binary and those who don't.

                    N Offline
                    N Offline
                    Nelek
                    wrote on last edited by
                    #22

                    I already have it for long time :-D ;P

                    M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you Rating helpful answers is nice, but saying thanks can be even nicer.

                    1 Reply Last reply
                    0
                    • A amagitech

                      I used same algorithm for c# and c++. I have read c++ is faster than c# but my tests shows c# is faster than c++. What's wrong? My codes like this. c++ is 36 seconds

                      #include
                      #include

                      using namespace std;

                      int main(){
                      const clock_t beginTime = clock();
                      for (int i = 2; i < 2000000; i++)
                      {
                      bool isPrime = true;
                      for (int j = 2; j*j <= i; j++)
                      {
                      if (i%j == 0){
                      isPrime = false;
                      break;
                      }
                      }
                      if (isPrime)
                      cout << i << endl;
                      }
                      cout << float(clock() - beginTime) / CLOCKS_PER_SEC;
                      }

                      c# is 9 seconds

                      using System;

                      namespace Console01
                      {
                      class Program
                      {

                          static void Main(string\[\] args)
                          {
                              DateTime beginTime = DateTime.Now;
                              for (int i = 2; i <= 2000000; i++)
                              {
                                  bool isPrime = true;
                                  for (int j = 2; j \* j < i; j++)
                                  {
                                      if (i % j == 0)
                                      {
                                          isPrime = false;
                                          break;
                                      }
                                  }
                                  if (isPrime)
                                      Console.WriteLine(i);
                              }
                              TimeSpan ts = DateTime.Now - beginTime;
                              Console.WriteLine(ts.Seconds);
                          }
                      
                      }
                      

                      }

                      P Offline
                      P Offline
                      PIEBALDconsult
                      wrote on last edited by
                      #23

                      C++ is filth. For real speed you need C. :cool:

                      R Sander RosselS 2 Replies Last reply
                      0
                      • P PIEBALDconsult

                        C++ is filth. For real speed you need C. :cool:

                        R Offline
                        R Offline
                        Rob Grainger
                        wrote on last edited by
                        #24

                        That depends a lot on whether you want to reuse components such as sort algorithms. I doubt very much you can match std::sort performance with C code, except for hand-coding every damn sort. Even then, C++ optimisation is damn fine nowadays.

                        "If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.

                        P 1 Reply Last reply
                        0
                        • R Rob Grainger

                          That depends a lot on whether you want to reuse components such as sort algorithms. I doubt very much you can match std::sort performance with C code, except for hand-coding every damn sort. Even then, C++ optimisation is damn fine nowadays.

                          "If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.

                          P Offline
                          P Offline
                          PIEBALDconsult
                          wrote on last edited by
                          #25

                          Rob Grainger wrote:

                          hand-coding every damn sort

                          As appropriate for the specific task, yes.

                          1 Reply Last reply
                          0
                          • A amagitech

                            I used same algorithm for c# and c++. I have read c++ is faster than c# but my tests shows c# is faster than c++. What's wrong? My codes like this. c++ is 36 seconds

                            #include
                            #include

                            using namespace std;

                            int main(){
                            const clock_t beginTime = clock();
                            for (int i = 2; i < 2000000; i++)
                            {
                            bool isPrime = true;
                            for (int j = 2; j*j <= i; j++)
                            {
                            if (i%j == 0){
                            isPrime = false;
                            break;
                            }
                            }
                            if (isPrime)
                            cout << i << endl;
                            }
                            cout << float(clock() - beginTime) / CLOCKS_PER_SEC;
                            }

                            c# is 9 seconds

                            using System;

                            namespace Console01
                            {
                            class Program
                            {

                                static void Main(string\[\] args)
                                {
                                    DateTime beginTime = DateTime.Now;
                                    for (int i = 2; i <= 2000000; i++)
                                    {
                                        bool isPrime = true;
                                        for (int j = 2; j \* j < i; j++)
                                        {
                                            if (i % j == 0)
                                            {
                                                isPrime = false;
                                                break;
                                            }
                                        }
                                        if (isPrime)
                                            Console.WriteLine(i);
                                    }
                                    TimeSpan ts = DateTime.Now - beginTime;
                                    Console.WriteLine(ts.Seconds);
                                }
                            
                            }
                            

                            }

                            G Offline
                            G Offline
                            Gjeltema
                            wrote on last edited by
                            #26

                            This link is a blog series done by Raymond Chen and Rico Mariani where Raymond wrote a Chinese dictionary in C++ and Rico wrote a naïve line-by-line translation of the C++ code in C#. The C# code blew away the C++ code in performance. Raymond then did a number of ever more severe optimizations (including writing his own string class) to finally beat the naïve C# performance. Rico then optimized the C# code and again beat the C++ performance. Raymond then did a couple of more optimizations to finally beat the C#. The summary is thus: Doing a straightforward implementation in C# will yield really good performance (and is much quicker to implement, relative to C++), in many cases better than a straightforward implementation in C++. But, if you put in a significant amount of optimization effort, C++ CAN clearly outperform the C# code - as noted in the final blog post of that series, there's certain initial overhead (60ms in Rico's case) that the CLR imposes that you cannot optimize away in C#.

                            P 1 Reply Last reply
                            0
                            • A amagitech

                              I used same algorithm for c# and c++. I have read c++ is faster than c# but my tests shows c# is faster than c++. What's wrong? My codes like this. c++ is 36 seconds

                              #include
                              #include

                              using namespace std;

                              int main(){
                              const clock_t beginTime = clock();
                              for (int i = 2; i < 2000000; i++)
                              {
                              bool isPrime = true;
                              for (int j = 2; j*j <= i; j++)
                              {
                              if (i%j == 0){
                              isPrime = false;
                              break;
                              }
                              }
                              if (isPrime)
                              cout << i << endl;
                              }
                              cout << float(clock() - beginTime) / CLOCKS_PER_SEC;
                              }

                              c# is 9 seconds

                              using System;

                              namespace Console01
                              {
                              class Program
                              {

                                  static void Main(string\[\] args)
                                  {
                                      DateTime beginTime = DateTime.Now;
                                      for (int i = 2; i <= 2000000; i++)
                                      {
                                          bool isPrime = true;
                                          for (int j = 2; j \* j < i; j++)
                                          {
                                              if (i % j == 0)
                                              {
                                                  isPrime = false;
                                                  break;
                                              }
                                          }
                                          if (isPrime)
                                              Console.WriteLine(i);
                                      }
                                      TimeSpan ts = DateTime.Now - beginTime;
                                      Console.WriteLine(ts.Seconds);
                                  }
                              
                              }
                              

                              }

                              D Offline
                              D Offline
                              Dave Kreskowiak
                              wrote on last edited by
                              #27

                              Rule #1 of bench-marking code like this is to make damn sure both pieces of code are on the same playing field. As you've already been told, they are not. NEVER EVER EVER put output code (WriteLine, cout, printf, setting a TextBox.Text property, ...) inside your timing code. If you need to print result strings, put the result values in a plain old, preallocated(!), array. Arrays works pretty much the same across all languages so there isn't much of an overhead difference between implementations. Output your data to the screen after you exit the timing code. This way, you're not timing the efficiency of the output code on top of your algorithm code. As you've seen, the differences between console, stream and even visual control implementations can be HUGE.

                              A guide to posting questions on CodeProject

                              Click this: Asking questions is a skill. Seriously, do it.
                              Dave Kreskowiak

                              J 1 Reply Last reply
                              0
                              • D Dave Kreskowiak

                                Rule #1 of bench-marking code like this is to make damn sure both pieces of code are on the same playing field. As you've already been told, they are not. NEVER EVER EVER put output code (WriteLine, cout, printf, setting a TextBox.Text property, ...) inside your timing code. If you need to print result strings, put the result values in a plain old, preallocated(!), array. Arrays works pretty much the same across all languages so there isn't much of an overhead difference between implementations. Output your data to the screen after you exit the timing code. This way, you're not timing the efficiency of the output code on top of your algorithm code. As you've seen, the differences between console, stream and even visual control implementations can be HUGE.

                                A guide to posting questions on CodeProject

                                Click this: Asking questions is a skill. Seriously, do it.
                                Dave Kreskowiak

                                J Offline
                                J Offline
                                jeron1
                                wrote on last edited by
                                #28

                                Dave Kreskowiak wrote:

                                NEVER EVER EVER

                                :laugh: Hmmm, sounds like you're waffling on this issue.

                                "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle

                                1 Reply Last reply
                                0
                                • Z ZurdoDev

                                  Personally, I see this more as a technical discussion than a programming question and therefore valid for the Lounge. :^) But I can see why you made the comment. Way more interesting than what is usually in the Lounge. :-\

                                  There are only 10 types of people in the world, those who understand binary and those who don't.

                                  J Offline
                                  J Offline
                                  Jeremy Falcon
                                  wrote on last edited by
                                  #29

                                  Agreed :thumbsup:

                                  Jeremy Falcon

                                  1 Reply Last reply
                                  0
                                  • P PIEBALDconsult

                                    C++ is filth. For real speed you need C. :cool:

                                    Sander RosselS Offline
                                    Sander RosselS Offline
                                    Sander Rossel
                                    wrote on last edited by
                                    #30

                                    C is filth. For real speed you need Assembler. :cool:

                                    Visit my blog at Sander's bits - Writing the code you need. Or read my articles at my CodeProject profile.

                                    Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

                                    Regards, Sander

                                    1 Reply Last reply
                                    0
                                    • G Gjeltema

                                      This link is a blog series done by Raymond Chen and Rico Mariani where Raymond wrote a Chinese dictionary in C++ and Rico wrote a naïve line-by-line translation of the C++ code in C#. The C# code blew away the C++ code in performance. Raymond then did a number of ever more severe optimizations (including writing his own string class) to finally beat the naïve C# performance. Rico then optimized the C# code and again beat the C++ performance. Raymond then did a couple of more optimizations to finally beat the C#. The summary is thus: Doing a straightforward implementation in C# will yield really good performance (and is much quicker to implement, relative to C++), in many cases better than a straightforward implementation in C++. But, if you put in a significant amount of optimization effort, C++ CAN clearly outperform the C# code - as noted in the final blog post of that series, there's certain initial overhead (60ms in Rico's case) that the CLR imposes that you cannot optimize away in C#.

                                      P Offline
                                      P Offline
                                      PIEBALDconsult
                                      wrote on last edited by
                                      #31

                                      Gjeltema wrote:

                                      is much quicker to implement

                                      Yes, and probably with lower maintenance requirements. As well as C# probably requiring a development team with an overall lower payroll. These are factors that really matter in real-world development. And if the result is "good enough", then call it done and move on.

                                      1 Reply Last reply
                                      0
                                      • A amagitech

                                        I used same algorithm for c# and c++. I have read c++ is faster than c# but my tests shows c# is faster than c++. What's wrong? My codes like this. c++ is 36 seconds

                                        #include
                                        #include

                                        using namespace std;

                                        int main(){
                                        const clock_t beginTime = clock();
                                        for (int i = 2; i < 2000000; i++)
                                        {
                                        bool isPrime = true;
                                        for (int j = 2; j*j <= i; j++)
                                        {
                                        if (i%j == 0){
                                        isPrime = false;
                                        break;
                                        }
                                        }
                                        if (isPrime)
                                        cout << i << endl;
                                        }
                                        cout << float(clock() - beginTime) / CLOCKS_PER_SEC;
                                        }

                                        c# is 9 seconds

                                        using System;

                                        namespace Console01
                                        {
                                        class Program
                                        {

                                            static void Main(string\[\] args)
                                            {
                                                DateTime beginTime = DateTime.Now;
                                                for (int i = 2; i <= 2000000; i++)
                                                {
                                                    bool isPrime = true;
                                                    for (int j = 2; j \* j < i; j++)
                                                    {
                                                        if (i % j == 0)
                                                        {
                                                            isPrime = false;
                                                            break;
                                                        }
                                                    }
                                                    if (isPrime)
                                                        Console.WriteLine(i);
                                                }
                                                TimeSpan ts = DateTime.Now - beginTime;
                                                Console.WriteLine(ts.Seconds);
                                            }
                                        
                                        }
                                        

                                        }

                                        P Offline
                                        P Offline
                                        PIEBALDconsult
                                        wrote on last edited by
                                        #32

                                        What dreadful code. Try a proper Sieve of Eratosthenes in both languages. Sieve of Eratosthenes - Wikipedia, the free encyclopedia[^] And, as the others have said, don't judge by IO. Also, run each several times, discard the highest and lowest times, and take the average. And use optimized builds. P.S. Streams is a big reason I do not use C++. X|

                                        1 Reply Last reply
                                        0
                                        • A amagitech

                                          I used same algorithm for c# and c++. I have read c++ is faster than c# but my tests shows c# is faster than c++. What's wrong? My codes like this. c++ is 36 seconds

                                          #include
                                          #include

                                          using namespace std;

                                          int main(){
                                          const clock_t beginTime = clock();
                                          for (int i = 2; i < 2000000; i++)
                                          {
                                          bool isPrime = true;
                                          for (int j = 2; j*j <= i; j++)
                                          {
                                          if (i%j == 0){
                                          isPrime = false;
                                          break;
                                          }
                                          }
                                          if (isPrime)
                                          cout << i << endl;
                                          }
                                          cout << float(clock() - beginTime) / CLOCKS_PER_SEC;
                                          }

                                          c# is 9 seconds

                                          using System;

                                          namespace Console01
                                          {
                                          class Program
                                          {

                                              static void Main(string\[\] args)
                                              {
                                                  DateTime beginTime = DateTime.Now;
                                                  for (int i = 2; i <= 2000000; i++)
                                                  {
                                                      bool isPrime = true;
                                                      for (int j = 2; j \* j < i; j++)
                                                      {
                                                          if (i % j == 0)
                                                          {
                                                              isPrime = false;
                                                              break;
                                                          }
                                                      }
                                                      if (isPrime)
                                                          Console.WriteLine(i);
                                                  }
                                                  TimeSpan ts = DateTime.Now - beginTime;
                                                  Console.WriteLine(ts.Seconds);
                                              }
                                          
                                          }
                                          

                                          }

                                          K Offline
                                          K Offline
                                          Kiriander
                                          wrote on last edited by
                                          #33

                                          Don't trust all what you read. Too many people think of C# as the early Java (that was painfully slow) or don't get what a JIT is really about.

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups