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 Offline
    D Offline
    dinh van hai
    wrote on last edited by
    #1

    hi there, I'm new to C# , I have a question abourt performence of c# application. Could you show me some ideal to improve performence ? Thank you, Sincerely,

    N 1 Reply Last reply
    0
    • D dinh van hai

      hi there, I'm new to C# , I have a question abourt performence of c# application. Could you show me some ideal to improve performence ? Thank you, Sincerely,

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

      Think out the fastest algorithm. Then implement it the fastest way. C# is very slow, and the compiler can optimize better than you so you can't do anything. Learn asm and you'll get the best performance, both of your mind and of your computer. The one and only Niklas Ulvinge aka IDK

      L M 2 Replies Last reply
      0
      • N Niklas Ulvinge

        Think out the fastest algorithm. Then implement it the fastest way. C# is very slow, and the compiler can optimize better than you so you can't do anything. Learn asm and you'll get the best performance, both of your mind and of your computer. The one and only Niklas Ulvinge aka IDK

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

        C# is very slow I doubt that.

        N 1 Reply Last reply
        0
        • L Lost User

          C# is very slow I doubt that.

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

          Try to make a loop that prints all the decimals in pi, or something else. Then make the same in qbasic (an interpreter!) and you'll see that even a twenty year old interpreter is faster than .Net. The one and only Niklas Ulvinge aka IDK

          L 1 Reply Last reply
          0
          • N Niklas Ulvinge

            Try to make a loop that prints all the decimals in pi, or something else. Then make the same in qbasic (an interpreter!) and you'll see that even a twenty year old interpreter is faster than .Net. The one and only Niklas Ulvinge aka IDK

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

            I didn't say .NET is on par or even faster than other languages, but stating that C# is "very slow" is absolutely not true. For example MDX applications are about 6 to 10 percent slower than their C++ counterparts, sometimes even less. That's actually pretty good considering all the .NET "overhead".

            N 1 Reply Last reply
            0
            • L Lost User

              I didn't say .NET is on par or even faster than other languages, but stating that C# is "very slow" is absolutely not true. For example MDX applications are about 6 to 10 percent slower than their C++ counterparts, sometimes even less. That's actually pretty good considering all the .NET "overhead".

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

              Have you ever tried what I descibed? I still think .net is the absolutly slowest platform I've ever programmed on. The one and only Niklas Ulvinge aka IDK

              D 1 Reply Last reply
              0
              • N Niklas Ulvinge

                Have you ever tried what I descibed? I still think .net is the absolutly slowest platform I've ever programmed on. The one and only Niklas Ulvinge aka IDK

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

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