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#
  4. Performence in c#

Performence in c#

Scheduled Pinned Locked Moved C#
questioncsharpcode-review
36 Posts 9 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.
  • D Daniel Grunwald

    Could you post your code (C# and QBASIC)? Because you must have done something extremly wrong in the C# version to get such a low performance.

    N Offline
    N Offline
    Niklas Ulvinge
    wrote on last edited by
    #8

    C#:

            static public void cpi()
            {
                do
                {
                    pi += 1 / a;
                    pi -= 1 / (a + 2);
                    a += 4;
                    Console.WriteLine(pi * 4);
                } while (true);
            }
    

    Qbasic:

    DEFDBL A-Z
    'DEFLNG B
    CLS
    b = 1
    DO
        ab = a
        a = a + 1 / b
        a = a - 1 / (b + 2)
        b = b + 4
        LOCATE 1, 1: PRINT a * 4, b
    LOOP
    

    The one and only Niklas Ulvinge aka IDK

    D L 2 Replies Last reply
    0
    • N Niklas Ulvinge

      C#:

              static public void cpi()
              {
                  do
                  {
                      pi += 1 / a;
                      pi -= 1 / (a + 2);
                      a += 4;
                      Console.WriteLine(pi * 4);
                  } while (true);
              }
      

      Qbasic:

      DEFDBL A-Z
      'DEFLNG B
      CLS
      b = 1
      DO
          ab = a
          a = a + 1 / b
          a = a - 1 / (b + 2)
          b = b + 4
          LOCATE 1, 1: PRINT a * 4, b
      LOOP
      

      The one and only Niklas Ulvinge aka IDK

      D Offline
      D Offline
      Daniel Grunwald
      wrote on last edited by
      #9

      The flaw in your benchmark is that 99% of the time are used for output of the results and less than 1% is needed for the calculation. You're not comparing C# to QBasic, you're comparing PRINT to Console.WriteLine. Console.WriteLine will look at the Windows settings for your preferred decimal separator character etc. You can't compare it to QBasic's PRINT. Moreover, a lot of time will be used by the console window to render the output. Here it can be a big difference if you reset the cursor position or let the window scroll. Try this: static public void cpi() { int start = Environment.TickCount; double pi = 0; double a = 1; for (int i = 0; i < 100000000; i++) { pi += 1 / a; pi -= 1 / (a + 2); a += 4; } Console.WriteLine(pi * 4); Console.WriteLine("Finished after " + (Environment.TickCount - start) + " ms"); Console.ReadKey(); } - against - DEFDBL A-Y DEFLNG Z CLS Y = TIMER b = 1 FOR Z = 0 TO 1000000 A = A + 1 / b A = A - 1 / (b + 2) b = b + 4 NEXT PRINT A * 4 PRINT TIMER - Y CSharp for 100000000 iterations: 1750 ms QBASIC for 1000000 iterations: 4 s This means C# is about 230 times as fast as QBASIC for raw calculations. One could still use C# and P/Invoke to an assembler-written PRINT function if you really need to output large amounts of doubles to the console.

      L N 2 Replies Last reply
      0
      • N Niklas Ulvinge

        C#:

                static public void cpi()
                {
                    do
                    {
                        pi += 1 / a;
                        pi -= 1 / (a + 2);
                        a += 4;
                        Console.WriteLine(pi * 4);
                    } while (true);
                }
        

        Qbasic:

        DEFDBL A-Z
        'DEFLNG B
        CLS
        b = 1
        DO
            ab = a
            a = a + 1 / b
            a = a - 1 / (b + 2)
            b = b + 4
            LOCATE 1, 1: PRINT a * 4, b
        LOOP
        

        The one and only Niklas Ulvinge aka IDK

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

        Could you please test the same without PRINT and Console.WriteLine?

        1 Reply Last reply
        0
        • D Daniel Grunwald

          The flaw in your benchmark is that 99% of the time are used for output of the results and less than 1% is needed for the calculation. You're not comparing C# to QBasic, you're comparing PRINT to Console.WriteLine. Console.WriteLine will look at the Windows settings for your preferred decimal separator character etc. You can't compare it to QBasic's PRINT. Moreover, a lot of time will be used by the console window to render the output. Here it can be a big difference if you reset the cursor position or let the window scroll. Try this: static public void cpi() { int start = Environment.TickCount; double pi = 0; double a = 1; for (int i = 0; i < 100000000; i++) { pi += 1 / a; pi -= 1 / (a + 2); a += 4; } Console.WriteLine(pi * 4); Console.WriteLine("Finished after " + (Environment.TickCount - start) + " ms"); Console.ReadKey(); } - against - DEFDBL A-Y DEFLNG Z CLS Y = TIMER b = 1 FOR Z = 0 TO 1000000 A = A + 1 / b A = A - 1 / (b + 2) b = b + 4 NEXT PRINT A * 4 PRINT TIMER - Y CSharp for 100000000 iterations: 1750 ms QBASIC for 1000000 iterations: 4 s This means C# is about 230 times as fast as QBASIC for raw calculations. One could still use C# and P/Invoke to an assembler-written PRINT function if you really need to output large amounts of doubles to the console.

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

          Dammit mine takes almost 2.7 seconds, what CPU do you have? I thought my 3.0 GHz should be fine :sigh: :-D

          N 1 Reply Last reply
          0
          • L Lost User

            Dammit mine takes almost 2.7 seconds, what CPU do you have? I thought my 3.0 GHz should be fine :sigh: :-D

            N Offline
            N Offline
            Niklas Ulvinge
            wrote on last edited by
            #12

            It's very slow. That's what it's meant to be. The one and only Niklas Ulvinge aka IDK

            1 Reply Last reply
            0
            • D Daniel Grunwald

              The flaw in your benchmark is that 99% of the time are used for output of the results and less than 1% is needed for the calculation. You're not comparing C# to QBasic, you're comparing PRINT to Console.WriteLine. Console.WriteLine will look at the Windows settings for your preferred decimal separator character etc. You can't compare it to QBasic's PRINT. Moreover, a lot of time will be used by the console window to render the output. Here it can be a big difference if you reset the cursor position or let the window scroll. Try this: static public void cpi() { int start = Environment.TickCount; double pi = 0; double a = 1; for (int i = 0; i < 100000000; i++) { pi += 1 / a; pi -= 1 / (a + 2); a += 4; } Console.WriteLine(pi * 4); Console.WriteLine("Finished after " + (Environment.TickCount - start) + " ms"); Console.ReadKey(); } - against - DEFDBL A-Y DEFLNG Z CLS Y = TIMER b = 1 FOR Z = 0 TO 1000000 A = A + 1 / b A = A - 1 / (b + 2) b = b + 4 NEXT PRINT A * 4 PRINT TIMER - Y CSharp for 100000000 iterations: 1750 ms QBASIC for 1000000 iterations: 4 s This means C# is about 230 times as fast as QBASIC for raw calculations. One could still use C# and P/Invoke to an assembler-written PRINT function if you really need to output large amounts of doubles to the console.

              N Offline
              N Offline
              Niklas Ulvinge
              wrote on last edited by
              #13

              That was the point. C#'s out func and a lot other funcs it got is really slow. The one and only Niklas Ulvinge aka IDK

              D R C 3 Replies Last reply
              0
              • N Niklas Ulvinge

                That was the point. C#'s out func and a lot other funcs it got is really slow. The one and only Niklas Ulvinge aka IDK

                D Offline
                D Offline
                Daniel Grunwald
                wrote on last edited by
                #14

                I have an AMD 64 (3500+) here. Oh, and I used a release build without integer overflow checks, which again is unfair since QBasic does not support those optimizations. But I think there's no way to create a fair benchmark. You can create a benchmark that shows pretty much anything if you want. Write to the console and QBasic is faster than C#. Use Math.Sin and C# is 50 times faster than Java 1.4. Construct lots of objects on the heap and C# is faster than C (without using memory pooling). So the best language always depends on what you want to do. If you need optimal performance, C is the way to go (maybe even use assembler for some sections). C# is pretty fast, but it's easy to ruin performance by using some slow method. But this is the same as in every language, some functions always are slow. Console.WriteLine is something that is not performance-critical in nearly all C# applications, so it didn't made sense for Microsoft to write a fast implementation. You get much better performance in C# if you write to a file instead of the console. As a side note, there are algorithms that need only 50 iterations to get PI so exactly as a double can store it. What other .NET functions do you think are slow and would be useful inside critical loops? Console.WriteLine is not one of those for me.

                N 1 Reply Last reply
                0
                • N Niklas Ulvinge

                  That was the point. C#'s out func and a lot other funcs it got is really slow. The one and only Niklas Ulvinge aka IDK

                  R Offline
                  R Offline
                  Robert Rohde
                  wrote on last edited by
                  #15

                  Have you only come to this forum to post such crap? Have you tried to understand what Daniel was saying? Console.WriteLine as a very complex beast. You can feed it with any kind of data and it will print it out different ways automatically depending on the set culture. Is QBasic also able to do this with its PRINT function?

                  N 1 Reply Last reply
                  0
                  • D Daniel Grunwald

                    I have an AMD 64 (3500+) here. Oh, and I used a release build without integer overflow checks, which again is unfair since QBasic does not support those optimizations. But I think there's no way to create a fair benchmark. You can create a benchmark that shows pretty much anything if you want. Write to the console and QBasic is faster than C#. Use Math.Sin and C# is 50 times faster than Java 1.4. Construct lots of objects on the heap and C# is faster than C (without using memory pooling). So the best language always depends on what you want to do. If you need optimal performance, C is the way to go (maybe even use assembler for some sections). C# is pretty fast, but it's easy to ruin performance by using some slow method. But this is the same as in every language, some functions always are slow. Console.WriteLine is something that is not performance-critical in nearly all C# applications, so it didn't made sense for Microsoft to write a fast implementation. You get much better performance in C# if you write to a file instead of the console. As a side note, there are algorithms that need only 50 iterations to get PI so exactly as a double can store it. What other .NET functions do you think are slow and would be useful inside critical loops? Console.WriteLine is not one of those for me.

                    N Offline
                    N Offline
                    Niklas Ulvinge
                    wrote on last edited by
                    #16

                    Writeline is important for me... It's my debugger... Now I write almost everyting in asm, as it's so fast and easy to get. It got enought documentation to make me sick, that's the way I love it. The one and only Niklas Ulvinge aka IDK

                    D C L 3 Replies Last reply
                    0
                    • R Robert Rohde

                      Have you only come to this forum to post such crap? Have you tried to understand what Daniel was saying? Console.WriteLine as a very complex beast. You can feed it with any kind of data and it will print it out different ways automatically depending on the set culture. Is QBasic also able to do this with its PRINT function?

                      N Offline
                      N Offline
                      Niklas Ulvinge
                      wrote on last edited by
                      #17

                      No, but it's faster. The one and only Niklas Ulvinge aka IDK

                      S 1 Reply Last reply
                      0
                      • N Niklas Ulvinge

                        Writeline is important for me... It's my debugger... Now I write almost everyting in asm, as it's so fast and easy to get. It got enought documentation to make me sick, that's the way I love it. The one and only Niklas Ulvinge aka IDK

                        D Offline
                        D Offline
                        Daniel Grunwald
                        wrote on last edited by
                        #18

                        I also like to use Console.WriteLine for debugging. WriteLine is slower than PRINT, but both write extremly faster than you can read the output. If your output is useful for debugging, it probably isn't so long that it can have any impact on the program.

                        N 1 Reply Last reply
                        0
                        • D Daniel Grunwald

                          I also like to use Console.WriteLine for debugging. WriteLine is slower than PRINT, but both write extremly faster than you can read the output. If your output is useful for debugging, it probably isn't so long that it can have any impact on the program.

                          N Offline
                          N Offline
                          Niklas Ulvinge
                          wrote on last edited by
                          #19

                          One example of the performance loss of the Console.WriteLine func is when debugging the program to calc pi. Since it goes in very fast loop it is very slow. If you think my program was a bad example, I got a NN program that's built in C#. It needs to write variables at runtime every loop. The one and only Niklas Ulvinge aka IDK

                          1 Reply Last reply
                          0
                          • N Niklas Ulvinge

                            No, but it's faster. The one and only Niklas Ulvinge aka IDK

                            S Offline
                            S Offline
                            Stanciu Vlad
                            wrote on last edited by
                            #20

                            I have only one reply : LOL But not a simple LOL, a very loud LOL. I have never seen such a stubborn man. Btw, Niklas I wish you luck in writing an ERP in assembler :) I hope you understand...because is a rough world out there...

                            N 1 Reply Last reply
                            0
                            • S Stanciu Vlad

                              I have only one reply : LOL But not a simple LOL, a very loud LOL. I have never seen such a stubborn man. Btw, Niklas I wish you luck in writing an ERP in assembler :) I hope you understand...because is a rough world out there...

                              N Offline
                              N Offline
                              Niklas Ulvinge
                              wrote on last edited by
                              #21

                              What would be hard to write an ERP in assembler? The one and only Niklas Ulvinge aka IDK

                              1 Reply Last reply
                              0
                              • N Niklas Ulvinge

                                That was the point. C#'s out func and a lot other funcs it got is really slow. The one and only Niklas Ulvinge aka IDK

                                C Offline
                                C Offline
                                Colin Angus Mackay
                                wrote on last edited by
                                #22

                                Niklas Ulvinge wrote:

                                C#'s out func and a lot other funcs it got is really slow.

                                But, that isn't C# it is the .NET Framework.


                                My: Blog | Photos "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucious

                                N 1 Reply Last reply
                                0
                                • C Colin Angus Mackay

                                  Niklas Ulvinge wrote:

                                  C#'s out func and a lot other funcs it got is really slow.

                                  But, that isn't C# it is the .NET Framework.


                                  My: Blog | Photos "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucious

                                  N Offline
                                  N Offline
                                  Niklas Ulvinge
                                  wrote on last edited by
                                  #23

                                  OK, I meant the .NET framework... C# is compiled to msil, wich is the .net framework language... Another way of writing it is: The out func of C# and a lot other funcs it got is really slow Wich meant what I meant... The one and only Niklas Ulvinge aka IDK

                                  C 1 Reply Last reply
                                  0
                                  • N Niklas Ulvinge

                                    Writeline is important for me... It's my debugger... Now I write almost everyting in asm, as it's so fast and easy to get. It got enought documentation to make me sick, that's the way I love it. The one and only Niklas Ulvinge aka IDK

                                    C Offline
                                    C Offline
                                    Colin Angus Mackay
                                    wrote on last edited by
                                    #24

                                    Niklas Ulvinge wrote:

                                    Writeline is important for me... It's my debugger...

                                    Is that important to your customer? will they be looking at debugging statements?


                                    My: Blog | Photos "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucious

                                    N 1 Reply Last reply
                                    0
                                    • N Niklas Ulvinge

                                      OK, I meant the .NET framework... C# is compiled to msil, wich is the .net framework language... Another way of writing it is: The out func of C# and a lot other funcs it got is really slow Wich meant what I meant... The one and only Niklas Ulvinge aka IDK

                                      C Offline
                                      C Offline
                                      Colin Angus Mackay
                                      wrote on last edited by
                                      #25

                                      What I'm getting at is that C# isn't slow. The particular method you are using is slow because it takes into account a whole host of things like culture, formatting options and so on. If you only ever want to output something in a very simple way then Console.WriteLine is too feature rich, and that is what you are paying for. There is nothing stopping you from providing your own implementation if you need speed.


                                      My: Blog | Photos "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucious

                                      N 1 Reply Last reply
                                      0
                                      • C Colin Angus Mackay

                                        Niklas Ulvinge wrote:

                                        Writeline is important for me... It's my debugger...

                                        Is that important to your customer? will they be looking at debugging statements?


                                        My: Blog | Photos "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucious

                                        N Offline
                                        N Offline
                                        Niklas Ulvinge
                                        wrote on last edited by
                                        #26

                                        I don't got any costomers... I don't think all the 2,3 million members on this site have... The one and only Niklas Ulvinge aka IDK

                                        C 1 Reply Last reply
                                        0
                                        • C Colin Angus Mackay

                                          What I'm getting at is that C# isn't slow. The particular method you are using is slow because it takes into account a whole host of things like culture, formatting options and so on. If you only ever want to output something in a very simple way then Console.WriteLine is too feature rich, and that is what you are paying for. There is nothing stopping you from providing your own implementation if you need speed.


                                          My: Blog | Photos "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucious

                                          N Offline
                                          N Offline
                                          Niklas Ulvinge
                                          wrote on last edited by
                                          #27

                                          Thats true. I didn't think of that... But how do I access the memmory at 0xA000 ? The one and only Niklas Ulvinge aka IDK

                                          C 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