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.
  • 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
                  • S Super Lloyd

                    Well... you know now how much of a lie it is! Arguably, theoretically, C++ should be faster because it has a better ahead of time compiler. But it's also relying on harder to optimize and write library and less elegant compiler constructs... which make it slower... However C# will slow down at 2 crucial point: - Start-up. C# start-up time is definitely, humanely perceptibly, slower (because of all the assemblies verification) - Allegedly real time code can only be reliably be written in C++ as it is, likely easier to write allocation free code, whereas realtime C# slow down on hidden object allocation (although its allocation algorithm are much faster than C++ ones) - Finally, for laugh, it's just an empty arguments that C++ developer have against C# developer. Surprisingly most of the random comparison code written by random developer make C# wins. But the occasional C++ zealots sometimes come back after a couple of day of profiling and disassembly with a marginally faster C++ version saying that the C++ version was... unoptimized! So... here you have it!

                    All in one Menu-Ribbon Bar DirectX for WinRT/C# since 2013! Taking over the world since 1371!

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

                    That's the way I see it. C++ as a religious belief:)

                    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

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

                      Thanks for the very informative link!

                      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
                        sbcLion
                        wrote on last edited by
                        #58

                        Hi, I compiled and ran the program at home using g++. The printing version was 5.6 seconds and a non printing version was 4.62 seconds. much better than both those times. C# also has the overhead on the .NET runtime. And the C++ program just has the c++ standary libray for overhead. c++ is fully compiled and c# is tokenized and still needs to be processed by the JIT.

                        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

                          H Offline
                          H Offline
                          hpcoder2
                          wrote on last edited by
                          #59

                          3rd point down is crucial. Newbies often forget to ramp up compiler optimisation to -O2 or -O3. Leaving it as the default optimisation level usually sucks.

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

                            }

                            U Offline
                            U Offline
                            User 8189615
                            wrote on last edited by
                            #60

                            std::endl is not equivalent to '\n' but also flushes the buffer. Try to change endl by '\n'

                            1 Reply Last reply
                            0
                            • Z ZurdoDev

                              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 Offline
                              J Offline
                              James Lonero
                              wrote on last edited by
                              #61

                              I'll Trump yours and give you a bit of Sanders.

                              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
                                jschell
                                wrote on last edited by
                                #62

                                Muharrem B. wrote:

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

                                Because "everyone" doesn't know what they are talking about when then make a statement like that. First of course in pantheon of programming speed is of small consequence. Microsoft, google and apple are not successful because of speed but rather because they make money. Businesses that don't make money do not last. And developers that think technology is more important than sales end up working for companies that don't make money. Second of course there can be actual business reasons where 'speed' is important but in the vast, vast majority of cases actually producing speed is based on factors besides just code and language. Third when in fact something needs to be faster it is experience not code that actually leads to faster solutions. Someone with years of experience is much more likely to produce a 'fast' system than someone without that experience regardless of technology choice. Keep in mind however that that is an average an not an absolute. Fourth, benchmarks, which is what you have, is pretty much useless in determining speed as far as it means anything in the business world. They often reflect nothing but one small aspect of the language and platform on which they run. That is if they are down well. Poorly done they often reflect a misunderstanding of how languages, platforms and even benchmarks work. Very rarely they even reflect a biased attempt to achieve a specific result. Fifth if one really wants to actually focus on professional programming then one should focus on understanding the basics on what it means to create a system and not language specifics. Basic one would be to understand that performance is impacted in the following way 1. Requirements, most impact 2. Design (including explicit and implicit.) 3. platform 4. language least impact Basic two would be to understand that if one wants to get more performance out of an application (not a system) and one can only focus on 4 in the above then one must do the following 1. Learn how to use an application profiler 2. Learn how to simulate real business data in the application 3. Run the profiler and determine where it might actually be possible to improve performance. Focusing on 1/2 in the above can achieve orders of magnitude impacts on performance while 4 can only achieve marginal percentage increases. After all of that my personal opinion is that if one had an unlimited amoun

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

                                  }

                                  Z Offline
                                  Z Offline
                                  zezba9000
                                  wrote on last edited by
                                  #63

                                  Because its not. 1) Did you run with the debugger attached? 2) Did you build in "release" configuration? 3) Are the builds both running in x86 or x64? 4) What C++ and C#/.NET compiler are you using? Almost nothing is faster in C# when doing numerical calculations like this. Usually only stuff where the GC has some kind of benefit in performance be delaying de-allocation.

                                  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
                                    pdohara
                                    wrote on last edited by
                                    #64

                                    C# or rather the .Net runtime does a number of things to make code "safer". These are things like bounds checking, type safety, etc. Some of that happens at compile time, but of necessity a lot of it happens at run time. This obviously takes time. Much of it can be "turned off" if you want to though you may not want to. If you want very tight control of timing then C++ and likely straight C is a better bet. For a variety of problems .Net (and therefore C#) is performant enough and "safer" than C or C++. It is good to have options. Pat O

                                    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