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. General Programming
  3. C / C++ / MFC
  4. Amazing C/C++ and C# and Python performance test

Amazing C/C++ and C# and Python performance test

Scheduled Pinned Locked Moved C / C++ / MFC
csharpc++pythontestingbeta-testing
4 Posts 3 Posters 6 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 Offline
    C Offline
    Code4Ever
    wrote on last edited by
    #1

    I tried to do a basic calculation for testing the performance of different programming languages: C/C++: (***3.05 s***)

    int main()
    {
    int start = clock();
    double result;
    //<...>
    for (size_t i = 1; i < 100000000; i++)
    {
    result = sin((i * 25) / i * i);
    }
    //<...>
    int end = clock();//Now check what amount of ticks we have now.
    //To get the time, just subtract start from end, and divide by CLOCKS_PER_SEC.
    std::cout << "it took " << end - start << "ticks std::endl;
    }

    C#: (***2.452 s***)

    using System.Diagnostics;

    class TestClass
    {
    static void Main(string[] args)
    {
    Stopwatch sw = new Stopwatch();
    double result;

        sw.Start();
        {
            for (int i = 1; i < 100000000; i++)
            {
                result = Math.Sin((i \* 25)/i \* i);
            }
        }
        sw.Stop();
        Console.WriteLine(sw.ElapsedMilliseconds.ToString());
    }
    

    }

    And the same calculation in Python took 22.14 s to complete Why is C# faster than C++?:confused::confused::confused:

    L CPalliniC 3 Replies Last reply
    0
    • C Code4Ever

      I tried to do a basic calculation for testing the performance of different programming languages: C/C++: (***3.05 s***)

      int main()
      {
      int start = clock();
      double result;
      //<...>
      for (size_t i = 1; i < 100000000; i++)
      {
      result = sin((i * 25) / i * i);
      }
      //<...>
      int end = clock();//Now check what amount of ticks we have now.
      //To get the time, just subtract start from end, and divide by CLOCKS_PER_SEC.
      std::cout << "it took " << end - start << "ticks std::endl;
      }

      C#: (***2.452 s***)

      using System.Diagnostics;

      class TestClass
      {
      static void Main(string[] args)
      {
      Stopwatch sw = new Stopwatch();
      double result;

          sw.Start();
          {
              for (int i = 1; i < 100000000; i++)
              {
                  result = Math.Sin((i \* 25)/i \* i);
              }
          }
          sw.Stop();
          Console.WriteLine(sw.ElapsedMilliseconds.ToString());
      }
      

      }

      And the same calculation in Python took 22.14 s to complete Why is C# faster than C++?:confused::confused::confused:

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

      I'd expect all "intermediate" calculations in C# to be performed as "int", since you have not supplied a single float or double on the right of the equation. And you neglected to display / verify "result".

      "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

      1 Reply Last reply
      0
      • C Code4Ever

        I tried to do a basic calculation for testing the performance of different programming languages: C/C++: (***3.05 s***)

        int main()
        {
        int start = clock();
        double result;
        //<...>
        for (size_t i = 1; i < 100000000; i++)
        {
        result = sin((i * 25) / i * i);
        }
        //<...>
        int end = clock();//Now check what amount of ticks we have now.
        //To get the time, just subtract start from end, and divide by CLOCKS_PER_SEC.
        std::cout << "it took " << end - start << "ticks std::endl;
        }

        C#: (***2.452 s***)

        using System.Diagnostics;

        class TestClass
        {
        static void Main(string[] args)
        {
        Stopwatch sw = new Stopwatch();
        double result;

            sw.Start();
            {
                for (int i = 1; i < 100000000; i++)
                {
                    result = Math.Sin((i \* 25)/i \* i);
                }
            }
            sw.Stop();
            Console.WriteLine(sw.ElapsedMilliseconds.ToString());
        }
        

        }

        And the same calculation in Python took 22.14 s to complete Why is C# faster than C++?:confused::confused::confused:

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

        Note that `Math.Sin` is implemented in native code, see the [Reference Source](https://referencesource.microsoft.com/#mscorlib/system/math.cs,84180d208256f9c3) (not for the implementation, but for the fact that there's no implementation there). So this isn't really a benchmark of C# vs C++, but one sine written in C++ vs another sine written in C++.

        1 Reply Last reply
        0
        • C Code4Ever

          I tried to do a basic calculation for testing the performance of different programming languages: C/C++: (***3.05 s***)

          int main()
          {
          int start = clock();
          double result;
          //<...>
          for (size_t i = 1; i < 100000000; i++)
          {
          result = sin((i * 25) / i * i);
          }
          //<...>
          int end = clock();//Now check what amount of ticks we have now.
          //To get the time, just subtract start from end, and divide by CLOCKS_PER_SEC.
          std::cout << "it took " << end - start << "ticks std::endl;
          }

          C#: (***2.452 s***)

          using System.Diagnostics;

          class TestClass
          {
          static void Main(string[] args)
          {
          Stopwatch sw = new Stopwatch();
          double result;

              sw.Start();
              {
                  for (int i = 1; i < 100000000; i++)
                  {
                      result = Math.Sin((i \* 25)/i \* i);
                  }
              }
              sw.Stop();
              Console.WriteLine(sw.ElapsedMilliseconds.ToString());
          }
          

          }

          And the same calculation in Python took 22.14 s to complete Why is C# faster than C++?:confused::confused::confused:

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          Both of the previous answers are correct. On my machine, with VS2019, this code

          static void Main(string[] args)
          {
          Stopwatch sw = new Stopwatch();
          double result=0.0;

            sw.Start();
            {
              for (int i = 1; i < 100000000; i++)
              {
                result += Math.Sin((i \* 25.0) / i \* i);
              }
            }
            sw.Stop();
            Console.WriteLine(sw.ElapsedMilliseconds.ToString());
            Console.WriteLine(string.Format("{0}",result));
          }
          

          produces

          4739
          -7,98150964297362

          While the following

          int main()
          {
          int start = clock();
          double result = 0.0;
          for (size_t i = 1; i < 100000000; i++)
          {
          result += sin((i * 25.0) / i * i);
          }
          int end = clock();//Now check what amount of ticks we have now.
          //To get the time, just subtract start from end, and divide by CLOCKS_PER_SEC.
          std::cout << (1000.0 * (end - start) / CLOCKS_PER_SEC) << "\n";
          std::cout << result << "\n";
          }

          outputs

          4153
          -7.98151

          "In testa che avete, Signor di Ceprano?" -- Rigoletto

          In testa che avete, signor di Ceprano?

          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