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 1 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.
  • 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
    Sascha Lefevre
    wrote on last edited by
    #7

    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 J H 3 Replies 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.

      S Offline
      S Offline
      Sascha Lefevre
      wrote on last edited by
      #8

      Good catch!

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

      S 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
        Jochen Arndt
        wrote on last edited by
        #9

        You successfully proofed the poor performance of C++ streams. Just try the C++ version with printf instead of streams and it should be faster.

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

          }

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

          Yee I tried printf("%i\n",i); it is 7 second. That's faster than coutt

          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
            Tomaz Stih 0
            wrote on last edited by
            #11

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

              }

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

              We all know that VB is faster than them both.

              cheers Chris Maunder

              N L D X 4 Replies 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);
                    }
                
                }
                

                }

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

                So much is impossible to tell. But, as others have mentioned, avoid for performance. There are some very clever template libraries that do printf like behaviour while retaining type-safety. Which are the best of both worlds. Apart from that, are you timing a release build? C++ typically optimises way more aggressively in release builds.

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

                1 Reply Last reply
                0
                • 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.

                        realJSOPR Offline
                        realJSOPR 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
                                          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