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. performance puzzle

performance puzzle

Scheduled Pinned Locked Moved The Lounge
questionc++performancecsharpcss
65 Posts 18 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.
  • M Mechanical

    Super Lloyd wrote:

    any C# performance tip is welcome!

    That is like looking for performance from Java: You won't get it. If you need (or want) performance, don't do retards' languages. I know this thread isn't meant to incite hatred against retards' languages.

    NULL

    P Offline
    P Offline
    peterchen
    wrote on last edited by
    #6

    *Beeep* Wrong, but thanks for playing. Are you open to evidence for my statement, or is your only possibility to accept it and weep?

    Agh! Reality! My Archnemesis![^]
    | FoldWithUs! | sighist | WhoIncludes - Analyzing C++ include file hierarchy

    M 1 Reply Last reply
    0
    • S Super Lloyd

      Well, I am able (if I concentrate hard) to read MSIL But I just can't read disassembly! Ok, I'm being lazy, it's almost the same... how do I get the disassembly of the JITted code?!

      A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.

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

      Super Lloyd wrote:

      how do I get the disassembly of the JITted code?!

      Like I said, throw an uncaught exception. Attach the debugger when it is thrown. To prevent certain optimizations, ensure that throwing the exceptions happens at a place that the compiler can't prove will be reached. Then open the disassembly view. (doesn't exist in express editions)

      S 2 Replies Last reply
      0
      • P peterchen

        *Beeep* Wrong, but thanks for playing. Are you open to evidence for my statement, or is your only possibility to accept it and weep?

        Agh! Reality! My Archnemesis![^]
        | FoldWithUs! | sighist | WhoIncludes - Analyzing C++ include file hierarchy

        M Offline
        M Offline
        Mechanical
        wrote on last edited by
        #8

        My statement was made because I had to maintain some Native code I wrote some years ago. I realized how retarded I had become doing retards' languages.

        peterchen wrote:

        Are you open to evidence for my statement

        I'm all ears (big, pointy ears).

        NULL

        S D P 3 Replies Last reply
        0
        • L Lost User

          Super Lloyd wrote:

          how do I get the disassembly of the JITted code?!

          Like I said, throw an uncaught exception. Attach the debugger when it is thrown. To prevent certain optimizations, ensure that throwing the exceptions happens at a place that the compiler can't prove will be reached. Then open the disassembly view. (doesn't exist in express editions)

          S Offline
          S Offline
          Super Lloyd
          wrote on last edited by
          #9

          I see, good idea, thanks!

          A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.

          1 Reply Last reply
          0
          • S Simon P Stevens

            Interesting - Can you post code? If it's too much for a post try http://pastebin.com/[^]

            Simon

            S Offline
            S Offline
            Super Lloyd
            wrote on last edited by
            #10

            I tried to simplify the C++ code by just pasting the C# loop in, and it's now slower! Further if I used WORD every where instead of native int it's now the same speed as C#!! How... interesting! Trying to find the simple and fast C++ version again... hold on a minute...

            A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.

            1 Reply Last reply
            0
            • M Mechanical

              My statement was made because I had to maintain some Native code I wrote some years ago. I realized how retarded I had become doing retards' languages.

              peterchen wrote:

              Are you open to evidence for my statement

              I'm all ears (big, pointy ears).

              NULL

              S Offline
              S Offline
              Slacker007
              wrote on last edited by
              #11

              Mechanical wrote:

              doing retards' languages

              Exactly what is a retards' languages?

              I 1 Reply Last reply
              0
              • S Super Lloyd

                I have a short program which check prime number. Run multiple time a single slow loop, to test performance. I have a C++, C# and F# version. (to be fair with C# the C++ version use fixed sized 32 bit integers, instead of native size int, but that doesn't seem to make a difference anyway) A few things puzzle me. 1. The c++ is definitely faster! my best C# tweak makes C++ 20% faster. That surprises me because C# is compiled too (at runtime, at the first run) and I am only looping over byte array and using int number, I am not doing interop, and even I use stackalloc and pointer in (unsafe) C# so there is no bound check. And the algorithm is so simple that it's hard to believe (but it must be true) that the C++ compiler optimize the generated code further than the JITted C# one... 2. creating an array with stackalloc result in faster loop (5%) than using fixed(&array[0]), in fact using fixed(&array[0]) has the same speed than using normal (safe and bound checked) array!! how come!?! 3. F# is 25% slower than C#. Well I guess I should not be too surprised and I guess that teach me the posts about amazing performance of F# have less to do with some "magic F# quality" and more to do with the heavy type inference / use of generic and "fast delegate" replacement, which is not really put to use in my sample. So, what is the question? err.. Well, any C# performance tip is welcome!

                A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.

                L Offline
                L Offline
                leppie
                wrote on last edited by
                #12

                Years back, I wrote identical implementations in C# and C for a Blowfish cipher. The C version had a managed C++ interface for .NET consumption. Then running both, I found the C version (even though in a .NET wrapper) was about 5 times faster than the C# code. I compared the C code's generated assembler to a hand written assembler version, and they were almost 100% identical (kudos for the C++ compiler here). Anyways, there is no way C# can ever get close to native performance when you are number crunching.

                ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x)))

                S 1 Reply Last reply
                0
                • S Slacker007

                  Mechanical wrote:

                  doing retards' languages

                  Exactly what is a retards' languages?

                  I Offline
                  I Offline
                  Indivara
                  wrote on last edited by
                  #13

                  I suppose it is whatever a certain individual in the back room uses for his 'software engineering'.

                  M 1 Reply Last reply
                  0
                  • I Indivara

                    I suppose it is whatever a certain individual in the back room uses for his 'software engineering'.

                    M Offline
                    M Offline
                    Mechanical
                    wrote on last edited by
                    #14

                    Indivara wrote:

                    I suppose it is whatever a certain individual in the back room uses for his 'software engineering'

                    Are you trying to please Big Brother ? If yes, then you are smart.

                    NULL

                    I 1 Reply Last reply
                    0
                    • S Simon P Stevens

                      Interesting - Can you post code? If it's too much for a post try http://pastebin.com/[^]

                      Simon

                      S Offline
                      S Offline
                      Super Lloyd
                      wrote on last edited by
                      #15

                      C++ ======================================

                      int _tmain(int argc, _TCHAR* argv[])
                      {
                      if (argc != 2)
                      {
                      std::cerr << "Usage:\tsieve [iterations]\n";
                      return 1;
                      };

                      size\_t NUM = \_wtoi(argv\[1\]);
                      DWORD dw = ::GetTickCount();
                      

                      #if ! CS-Speed
                      char primes[8192+1];
                      int pbegin = 0;
                      int begin = 2;
                      int end = 8193;

                      while (NUM -- != 0)
                      {
                      		for (int i = 0; i < end; i++)
                      		{
                      				primes\[i\] = 1;
                      		}
                      
                      		for (int i = begin; i < end; ++i)
                      		{
                      				if (primes\[i\] != 0)
                      				{
                      						int p = i; // using this extra variable speeds up C++!!! (and slow down C# if I do it)
                      						for (int k = i + p; k < end; k += p)
                      						{
                      								primes\[k\] = 0;
                      						}
                      				}
                      		}
                      }
                      

                      #else
                      WORD end = 8193;
                      char primes[8193];
                      while (NUM-- != 0)
                      {
                      for (WORD i = 0; i < end; i++)
                      primes[i] = 1;

                      	for (WORD i = 2; i < end; ++i)
                      		if (primes\[i\] != 0)
                      			for (WORD k = 2 \* i; k < end; k += i)
                      				primes\[k\] = 0;
                      }
                      

                      #endif

                      DWORD dw2 = ::GetTickCount();
                      std::cout << "Milliseconds = " << dw2-dw << std::endl;
                      return 0;
                      

                      }

                      ====================================== C# ======================================

                      unsafe static void Main(string[] args)
                      {
                      if (args.Length != 1)
                      {
                      Console.WriteLine("Usage:\tsieve [iterations]");
                      return;
                      }

                      int NUM = int.Parse(args\[0\]);
                      var t0 = DateTime.Now;
                      
                      int end = 8193;
                      var primes = stackalloc byte\[end\];
                      while (NUM-- != 0)
                      {
                      	for (int i = 0; i < end; i++)
                      		primes\[i\] = 1;
                      
                      	for (int i = 2; i < end; ++i)
                      		if (primes\[i\] != 0)
                      			for (int k = 2 \* i; k < end; k += i)
                      				primes\[k\] = 0;
                      }
                      
                      var dt = DateTime.Now - t0;
                      System.Console.WriteLine("Milliseconds = {0}", dt.TotalMilliseconds);
                      

                      }

                      ====================================== F# ======================================

                      open System

                      [<EntryPoint>]
                      let main args =

                      let NUM = Int32.Parse args.\[0\]
                      let t0 = DateTime.Now
                      
                      let primes : byte\[\] = Array.zeroCreate ( 8192 + 1 )
                      let aend = 8192
                      for nloop = 1 to NUM do
                      
                          for i = 0 to aend do
                              primes.\[i\] <- 1uy
                      
                          for i = 2 to aend do
                              if primes.\[i\] <> 0uy then
                                  let mutable k = 2 \* i
                                  while k <= aend do
                                      primes.\[k\] <- 0uy
                                      k <- k + i
                      
                      
                      let dt = DateTime.Now - t0
                      Console.WriteLine("Milliseconds = {0}", dt.TotalMilliseconds)
                      0
                      

                      ===============================

                      S D 2 Replies Last reply
                      0
                      • S Super Lloyd

                        I have a short program which check prime number. Run multiple time a single slow loop, to test performance. I have a C++, C# and F# version. (to be fair with C# the C++ version use fixed sized 32 bit integers, instead of native size int, but that doesn't seem to make a difference anyway) A few things puzzle me. 1. The c++ is definitely faster! my best C# tweak makes C++ 20% faster. That surprises me because C# is compiled too (at runtime, at the first run) and I am only looping over byte array and using int number, I am not doing interop, and even I use stackalloc and pointer in (unsafe) C# so there is no bound check. And the algorithm is so simple that it's hard to believe (but it must be true) that the C++ compiler optimize the generated code further than the JITted C# one... 2. creating an array with stackalloc result in faster loop (5%) than using fixed(&array[0]), in fact using fixed(&array[0]) has the same speed than using normal (safe and bound checked) array!! how come!?! 3. F# is 25% slower than C#. Well I guess I should not be too surprised and I guess that teach me the posts about amazing performance of F# have less to do with some "magic F# quality" and more to do with the heavy type inference / use of generic and "fast delegate" replacement, which is not really put to use in my sample. So, what is the question? err.. Well, any C# performance tip is welcome!

                        A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.

                        C Offline
                        C Offline
                        CPallini
                        wrote on last edited by
                        #16

                        I think that even plain C is best suited for such a task. I suppose you may have performace boosts, from higher level language, only if they allow you to implement a better algorithm for a complex problem (I mean, whenever implementing the same algorithm C++ is too difficult because you have to take care of too many details, etc...). In any case, C# was created to speed up code development (without loosing too much performance), right? :)

                        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                        [My articles]

                        S D 3 Replies Last reply
                        0
                        • M Mechanical

                          Indivara wrote:

                          I suppose it is whatever a certain individual in the back room uses for his 'software engineering'

                          Are you trying to please Big Brother ? If yes, then you are smart.

                          NULL

                          I Offline
                          I Offline
                          Indivara
                          wrote on last edited by
                          #17

                          OK it looks like this conversation is going no further in the lounge...

                          1 Reply Last reply
                          0
                          • L leppie

                            Years back, I wrote identical implementations in C# and C for a Blowfish cipher. The C version had a managed C++ interface for .NET consumption. Then running both, I found the C version (even though in a .NET wrapper) was about 5 times faster than the C# code. I compared the C code's generated assembler to a hand written assembler version, and they were almost 100% identical (kudos for the C++ compiler here). Anyways, there is no way C# can ever get close to native performance when you are number crunching.

                            ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x)))

                            S Offline
                            S Offline
                            Super Lloyd
                            wrote on last edited by
                            #18

                            As I understood it, theoretically there is no reason why .NET shouldn't even be better, as it is compiled just for the machine it is executing on! That's what they say in their propaganda! I guess it depends on how much optimization they can run during the JIT compilation. But this algorithm is so simple, I thought there wasn't much left to optimize!!! I guess just one innocent instruction can make a difference when looped over a huge number of times... BTW, C#4 is faster! :)

                            A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.

                            1 Reply Last reply
                            0
                            • C CPallini

                              I think that even plain C is best suited for such a task. I suppose you may have performace boosts, from higher level language, only if they allow you to implement a better algorithm for a complex problem (I mean, whenever implementing the same algorithm C++ is too difficult because you have to take care of too many details, etc...). In any case, C# was created to speed up code development (without loosing too much performance), right? :)

                              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                              [My articles]

                              S Offline
                              S Offline
                              Super Lloyd
                              wrote on last edited by
                              #19

                              indeed, but they had this propaganda that .NET is JIT compiled just for the machine it's running on, so that theoretically it can even be faster! (it's what they used to say) Further C# 4 is way faster and this algorithm is so simple I can hardly see why the JITted version couldn't be as good as the compiled C version... guess I was wrong.. guess when things are looped over a huge number of time, just 1 instruction can make a big difference....

                              A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.

                              C 1 Reply Last reply
                              0
                              • M Mechanical

                                My statement was made because I had to maintain some Native code I wrote some years ago. I realized how retarded I had become doing retards' languages.

                                peterchen wrote:

                                Are you open to evidence for my statement

                                I'm all ears (big, pointy ears).

                                NULL

                                D Offline
                                D Offline
                                Dan Mos
                                wrote on last edited by
                                #20

                                Mechanical wrote:

                                I had to maintain some Native code I wrote some years ago. I realized how retarded I had become doing retards' languages.

                                Although I don't think much, your statement is wrong. Why? Simply because the reverse of it would be true also. Meaning that if you used to work on retard language, then went genius language for 4 or so years, if you would be asked to maintain a retard language piece of code you wouldn't be able to do it wright away. You would be a retard genius in a retard language. :)

                                I used to think.... Finally I realized it's no good.

                                1 Reply Last reply
                                0
                                • C CPallini

                                  I think that even plain C is best suited for such a task. I suppose you may have performace boosts, from higher level language, only if they allow you to implement a better algorithm for a complex problem (I mean, whenever implementing the same algorithm C++ is too difficult because you have to take care of too many details, etc...). In any case, C# was created to speed up code development (without loosing too much performance), right? :)

                                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                                  [My articles]

                                  S Offline
                                  S Offline
                                  Super Lloyd
                                  wrote on last edited by
                                  #21

                                  Plus I just bought a WP7 and some game take up to a minute to load, and I was wondering about optimization, and I was wondering if they plan to do ahead of time compilation of apps when deployed (instead of JIT) and I was wondering how good is the performance, etc...

                                  A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.

                                  1 Reply Last reply
                                  0
                                  • C CPallini

                                    I think that even plain C is best suited for such a task. I suppose you may have performace boosts, from higher level language, only if they allow you to implement a better algorithm for a complex problem (I mean, whenever implementing the same algorithm C++ is too difficult because you have to take care of too many details, etc...). In any case, C# was created to speed up code development (without loosing too much performance), right? :)

                                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                                    [My articles]

                                    D Offline
                                    D Offline
                                    Dan Mos
                                    wrote on last edited by
                                    #22

                                    :thumbsup:

                                    I used to think.... Finally I realized it's no good.

                                    1 Reply Last reply
                                    0
                                    • S Super Lloyd

                                      C++ ======================================

                                      int _tmain(int argc, _TCHAR* argv[])
                                      {
                                      if (argc != 2)
                                      {
                                      std::cerr << "Usage:\tsieve [iterations]\n";
                                      return 1;
                                      };

                                      size\_t NUM = \_wtoi(argv\[1\]);
                                      DWORD dw = ::GetTickCount();
                                      

                                      #if ! CS-Speed
                                      char primes[8192+1];
                                      int pbegin = 0;
                                      int begin = 2;
                                      int end = 8193;

                                      while (NUM -- != 0)
                                      {
                                      		for (int i = 0; i < end; i++)
                                      		{
                                      				primes\[i\] = 1;
                                      		}
                                      
                                      		for (int i = begin; i < end; ++i)
                                      		{
                                      				if (primes\[i\] != 0)
                                      				{
                                      						int p = i; // using this extra variable speeds up C++!!! (and slow down C# if I do it)
                                      						for (int k = i + p; k < end; k += p)
                                      						{
                                      								primes\[k\] = 0;
                                      						}
                                      				}
                                      		}
                                      }
                                      

                                      #else
                                      WORD end = 8193;
                                      char primes[8193];
                                      while (NUM-- != 0)
                                      {
                                      for (WORD i = 0; i < end; i++)
                                      primes[i] = 1;

                                      	for (WORD i = 2; i < end; ++i)
                                      		if (primes\[i\] != 0)
                                      			for (WORD k = 2 \* i; k < end; k += i)
                                      				primes\[k\] = 0;
                                      }
                                      

                                      #endif

                                      DWORD dw2 = ::GetTickCount();
                                      std::cout << "Milliseconds = " << dw2-dw << std::endl;
                                      return 0;
                                      

                                      }

                                      ====================================== C# ======================================

                                      unsafe static void Main(string[] args)
                                      {
                                      if (args.Length != 1)
                                      {
                                      Console.WriteLine("Usage:\tsieve [iterations]");
                                      return;
                                      }

                                      int NUM = int.Parse(args\[0\]);
                                      var t0 = DateTime.Now;
                                      
                                      int end = 8193;
                                      var primes = stackalloc byte\[end\];
                                      while (NUM-- != 0)
                                      {
                                      	for (int i = 0; i < end; i++)
                                      		primes\[i\] = 1;
                                      
                                      	for (int i = 2; i < end; ++i)
                                      		if (primes\[i\] != 0)
                                      			for (int k = 2 \* i; k < end; k += i)
                                      				primes\[k\] = 0;
                                      }
                                      
                                      var dt = DateTime.Now - t0;
                                      System.Console.WriteLine("Milliseconds = {0}", dt.TotalMilliseconds);
                                      

                                      }

                                      ====================================== F# ======================================

                                      open System

                                      [<EntryPoint>]
                                      let main args =

                                      let NUM = Int32.Parse args.\[0\]
                                      let t0 = DateTime.Now
                                      
                                      let primes : byte\[\] = Array.zeroCreate ( 8192 + 1 )
                                      let aend = 8192
                                      for nloop = 1 to NUM do
                                      
                                          for i = 0 to aend do
                                              primes.\[i\] <- 1uy
                                      
                                          for i = 2 to aend do
                                              if primes.\[i\] <> 0uy then
                                                  let mutable k = 2 \* i
                                                  while k <= aend do
                                                      primes.\[k\] <- 0uy
                                                      k <- k + i
                                      
                                      
                                      let dt = DateTime.Now - t0
                                      Console.WriteLine("Milliseconds = {0}", dt.TotalMilliseconds)
                                      0
                                      

                                      ===============================

                                      S Offline
                                      S Offline
                                      Simon P Stevens
                                      wrote on last edited by
                                      #23

                                      Cool. I'm looking at it now. I'll post back in a bit.

                                      Simon

                                      S 1 Reply Last reply
                                      0
                                      • S Super Lloyd

                                        indeed, but they had this propaganda that .NET is JIT compiled just for the machine it's running on, so that theoretically it can even be faster! (it's what they used to say) Further C# 4 is way faster and this algorithm is so simple I can hardly see why the JITted version couldn't be as good as the compiled C version... guess I was wrong.. guess when things are looped over a huge number of time, just 1 instruction can make a big difference....

                                        A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.

                                        C Offline
                                        C Offline
                                        CPallini
                                        wrote on last edited by
                                        #24

                                        Super Lloyd wrote:

                                        indeed, but they had this propaganda that .NET is JIT compiled just for the machine it's running on, so that theoretically it can even be faster!

                                        I guess this happens also for C++, in this case. :)

                                        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                                        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                                        [My articles]

                                        S 1 Reply Last reply
                                        0
                                        • C CPallini

                                          Super Lloyd wrote:

                                          indeed, but they had this propaganda that .NET is JIT compiled just for the machine it's running on, so that theoretically it can even be faster!

                                          I guess this happens also for C++, in this case. :)

                                          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                                          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                                          [My articles]

                                          S Offline
                                          S Offline
                                          Super Lloyd
                                          wrote on last edited by
                                          #25

                                          Mmm... indeed! Silly me! :laugh:

                                          A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.

                                          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