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.
  • C Chris Maunder

    We all know that VB is faster than them both.

    cheers Chris Maunder

    X Offline
    X Offline
    xiecsuk
    wrote on last edited by
    #36

    Let's hear it for VB. Hip hip

    1 Reply Last reply
    0
    • S Sascha Lefevre

      I'm not an expert in this but I can tell you this much: - You didn't set a processor affinity. Stack swaps could have skewed your results. - You didn't set the process and thread priority to high. Your programs code have been interrupted by other threads. - Did you make sure you ran both programs with compiler optimization and without debugger/profiler attached? - Integer-arithmetic isn't representative for the overall speed of the language. Take a look here: Head-to-head benchmark: C++ vs .NET[^]

      If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

      S Offline
      S Offline
      Stefan_Lang
      wrote on last edited by
      #37

      That article provides a rather thorough comparison, but it focuses on library implementations, not the core language. Just from skimming over the results I spotted several remarks on 'lousy' implementations, and that alone is a dead giveaway that the results should be taken with a huge pint of salt.

      GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

      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);
            }
        
        }
        

        }

        I Offline
        I Offline
        irneb
        wrote on last edited by
        #38

        As many others have also shown, your main issue is the cout streaming in C++ is less than optimal (to say the least). Also things like compiling to production settings (i.e. all optimizations possible) instead of debug, ensuring thread priority is set to high and cpu affinity set to ensure no other processes interfere and cause a cache reload during the test. All these thing could throw out any such benchmarking. But just to be nitpicky: You're using a simple clock to time your benchmark. In both C++ as well as C#, that has previously been seen to have lots of inaccuracies due to optimizations in the clock's implementation. To ensure the most accurate such timing (if you're not using a profiler instead - which BTW you should rather do) then rather use: - In C++ it depends on the system. Under Linux clock_gettime[^], under Windows QPC [^] is a better alternative - In C# you should be using Stopwatch[^] instead.

        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);
              }
          
          }
          

          }

          J Offline
          J Offline
          Jonas Hammarberg
          wrote on last edited by
          #39

          Comparing apples and pears ... **c** for (int i = 2; i **<** 2000000; i++) for (int j = 2; j*j **<=** i; j++) **c#** for (int i = 2; i **<=** 2000000; i++) for (int j = 2; j * j **<** i; j++) As it stands now, C does quite a few more loops than C# ... Change the loops (so that they are the same) and remove I/O from inside them. YMMV

          A 1 Reply Last reply
          0
          • J Jonas Hammarberg

            Comparing apples and pears ... **c** for (int i = 2; i **<** 2000000; i++) for (int j = 2; j*j **<=** i; j++) **c#** for (int i = 2; i **<=** 2000000; i++) for (int j = 2; j * j **<** i; j++) As it stands now, C does quite a few more loops than C# ... Change the loops (so that they are the same) and remove I/O from inside them. YMMV

            A Offline
            A Offline
            amagitech
            wrote on last edited by
            #40

            you righ'tit escaped me. I fixed now c# is 7 second.

            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);
                  }
              
              }
              

              }

              T Offline
              T Offline
              Thornik
              wrote on last edited by
              #41

              Any benchmark test never ever should include I/O! You're not experienced here, so we forgive you. :)

              A 1 Reply Last reply
              0
              • T Thornik

                Any benchmark test never ever should include I/O! You're not experienced here, so we forgive you. :)

                A Offline
                A Offline
                amagitech
                wrote on last edited by
                #42

                :) I am junior developer. I need to learn more software culture. But I don't have any teacher(who is senior developer) for learning this subjects. So I take risk to asking dummy questions :)

                1 Reply Last reply
                0
                • S Sascha Lefevre

                  Good catch!

                  If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                  S Offline
                  S Offline
                  SortaCore
                  wrote on last edited by
                  #43

                  Also, you're using < in one for loop, and <= in the other.

                  1 Reply Last reply
                  0
                  • R realJSOP

                    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 Offline
                    Y Offline
                    Ygnaiih
                    wrote on last edited by
                    #44

                    So liberals are not permitted here. I'm not a liberal, but I'm sure that sooner or later I'll say something that is not extreme right wing enough to satisfy you.

                    Z 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);
                          }
                      
                      }
                      

                      }

                      L Offline
                      L Offline
                      loctrice
                      wrote on last edited by
                      #45

                      All the comments are like "but you didn't do it this way" or "you didn't do it that way". So that pretty much just says that you can easily write c# code to be fast, and in many cases faster than c++. However if you want to obsess over your code optimization and use uncommon coding practices you can make c++ be faster than c# in many cases.

                      Elephant elephant elephant, sunshine sunshine sunshine

                      1 Reply Last reply
                      0
                      • Y Ygnaiih

                        So liberals are not permitted here. I'm not a liberal, but I'm sure that sooner or later I'll say something that is not extreme right wing enough to satisfy you.

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

                        Ygnaiih wrote:

                        So liberals are not permitted here.

                        Only a liberal would have made that leap. :-\

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

                        Y 1 Reply Last reply
                        0
                        • Z ZurdoDev

                          Ygnaiih wrote:

                          So liberals are not permitted here.

                          Only a liberal would have made that leap. :-\

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

                          Y Offline
                          Y Offline
                          Ygnaiih
                          wrote on last edited by
                          #47

                          Told you up front I'm not a liberal. My point is if you want to do a political discussion, code project is not the place for it.

                          Z 1 Reply Last reply
                          0
                          • Y Ygnaiih

                            Told you up front I'm not a liberal. My point is if you want to do a political discussion, code project is not the place for it.

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

                            Ygnaiih wrote:

                            Told you up front I'm not a liberal

                            Ya, I was making a silly joke.

                            Ygnaiih wrote:

                            if you want to do a political discussion, code project is not the place for it.

                            Actually, it is, but not in the Lounge. The Soapbox is the place for it. However, John wasn't making any political statements. :doh:

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

                            J 1 Reply Last reply
                            0
                            • A Anthony Mushrow

                              After a quick check, what you're really comparing here is the efficiency of Console.WriteLine vs cout If you swap cout for printf you get much more similar times. In my case the C++ was a second faster than the C# but it's not a very fair comparison. Really I don't think you'll find much difference in performance of either.

                              R Offline
                              R Offline
                              Rio Rico Rick
                              wrote on last edited by
                              #49

                              An answer to the question? What's wrong with you!!! :-) It's funny how it was so horrible that this question was here, but the whole long conversation about liberals and stuff is just fine! Hmmm people just being people again. It's a shame the way we act sometimes.

                              hatfok King Yiddum's Castle Pegasus Galaxy

                              1 Reply Last reply
                              0
                              • A Anthony Mushrow

                                After a quick check, what you're really comparing here is the efficiency of Console.WriteLine vs cout If you swap cout for printf you get much more similar times. In my case the C++ was a second faster than the C# but it's not a very fair comparison. Really I don't think you'll find much difference in performance of either.

                                P Offline
                                P Offline
                                petek1955
                                wrote on last edited by
                                #50

                                Yes, it's comparing Console.WriteLine with cout, but even then it's not a fair comparison. std::endl doesn't just write a newline character, it also flushes the output buffer. Try replacing endl with '\n'. Also try redirecting the console output to a file, running each program several times, discarding the outliers and taking an average of the remaining run times then come back with your results. I doubt that there will be much difference between the two programs because basic C++ streams are not as bad as some people here are trying to make out.

                                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);
                                      }
                                  
                                  }
                                  

                                  }

                                  H Offline
                                  H Offline
                                  hal23x
                                  wrote on last edited by
                                  #51

                                  I copied and pasted your exact code and compiled them with the Visual Studio 2008 (v9.0) compilers (cl for C++, csc for C#) and got wildly different results--C++ reported a time of 2.868 seconds, versus 4 for C#. My exact command lines were: For C++:

                                  cl /EHcs cpptest.cpp

                                  cpptest.exe > cppoutput.txt

                                  For C#:

                                  csc cstest.cs

                                  cstest.exe > csoutput.txt

                                  As far as why people say C++ is faster than C#--there are a great many factors, including how the code was written, what the code is doing, how it was compiled, the system (hardware and OS) it's run on, compilers, etc. that can make a difference, and no doubt there may be examples of C# performing some things faster in some instances. In general, I think it has more to do with the overhead inherent in C#, but that's just me speculating. Maybe we can get a real answer from someone knows more than I do about the internals of the languages (and whether managed C++/CLI would have had results similar to the C# code above).

                                  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);
                                        }
                                    
                                    }
                                    

                                    }

                                    S Offline
                                    S Offline
                                    Sharath Shetty
                                    wrote on last edited by
                                    #52

                                    This is like challenging a boxing champion to an arm-wrestling competition and then find him wanting. :laugh: The speed of C++ is not tested by printing into stdout. Do some processing that doesn't involve printing repeatedly into console. For example, calculate the largest 10 digit prime by traversing a 5x5 number matrix. Then compare the speed.

                                    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);
                                          }
                                      
                                      }
                                      

                                      }

                                      M Offline
                                      M Offline
                                      Member 12023988
                                      wrote on last edited by
                                      #53

                                      You're timing cout.

                                      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);
                                            }
                                        
                                        }
                                        

                                        }

                                        M Offline
                                        M Offline
                                        Member 12023988
                                        wrote on last edited by
                                        #54

                                        "my tests shows c# is faster than c++" No they don't. You showed that ONE C# program ran faster than ONE C++ program. Sheesh.

                                        1 Reply Last reply
                                        0
                                        • A Anthony Mushrow

                                          After a quick check, what you're really comparing here is the efficiency of Console.WriteLine vs cout If you swap cout for printf you get much more similar times. In my case the C++ was a second faster than the C# but it's not a very fair comparison. Really I don't think you'll find much difference in performance of either.

                                          J Offline
                                          J Offline
                                          jrobb229
                                          wrote on last edited by
                                          #55

                                          Thanks for getting back on topic. While questioning c++ is perhaps not PC, it is relevant to programmers (given the marked difference in development time.)

                                          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