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. Performance difference??

Performance difference??

Scheduled Pinned Locked Moved C#
csharpperformancequestion
15 Posts 6 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.
  • S Shy Agam

    I see... Is it the same for reference types?

    L Offline
    L Offline
    Luc Pattyn
    wrote on last edited by
    #6

    Hi, please read my other reply first. if it is:

    string x="aha";

    for (...) {
    string y=x;
    }

    then again it wont make a difference, since y=x simply copies a reference, it does not create an object; it has the same cost as int y=x; And again, if the compiler notices x can not change during the loop, it should and would move the statement outside the loop. :)

    Luc Pattyn


    try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


    S 1 Reply Last reply
    0
    • L Luc Pattyn

      Hi, please read my other reply first. if it is:

      string x="aha";

      for (...) {
      string y=x;
      }

      then again it wont make a difference, since y=x simply copies a reference, it does not create an object; it has the same cost as int y=x; And again, if the compiler notices x can not change during the loop, it should and would move the statement outside the loop. :)

      Luc Pattyn


      try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


      S Offline
      S Offline
      Shy Agam
      wrote on last edited by
      #7

      Exactly the answer I was looking for! ;) Thanks!

      1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, you should not believe everything they tell you. int is a value type, it probably gets "allocated" on stack (meaning the stack pointer gets lowered a bit more when you enter this method to reserve a spot for it), this has zero cost. (the probably means, maybe the int variable does not need storage, the JIT might decide to keep it in one of the CPU registers, but I am not relying on this) Anyway, there is no "new", no object, and no garbage collection involved. both code snippets could and should result in exactly the same MSIL code; and I would be very surprised if you can measure the slightest difference in execution time (which would hint a compiler inefficiency, not an inherent difference between both code snippets). :) :)

        Luc Pattyn


        try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


        B Offline
        B Offline
        BoneSoft
        wrote on last edited by
        #8

        I stand corrected, both of these...

        public void Inside() {
        for (int i = 0; i < 1000; i++) {
        int x = i;
        }
        }

        public void Outside() {
        int x;
        for (int i = 0; i < 1000; i++) {
        x = i;
        }
        }

        decompile to something similar to this...

        public void Outside() {
        int num1;
        int num2;
        for (num2 = 0; (num2 < 1000); num2 = (num2 + 1)) {
        num1 = num2;
        }
        }

        The compiler does you the favor of moving your variable outside the loop where it belongs.


        Try code model generation tools at BoneSoft.com.

        L 1 Reply Last reply
        0
        • B BoneSoft

          I stand corrected, both of these...

          public void Inside() {
          for (int i = 0; i < 1000; i++) {
          int x = i;
          }
          }

          public void Outside() {
          int x;
          for (int i = 0; i < 1000; i++) {
          x = i;
          }
          }

          decompile to something similar to this...

          public void Outside() {
          int num1;
          int num2;
          for (num2 = 0; (num2 < 1000); num2 = (num2 + 1)) {
          num1 = num2;
          }
          }

          The compiler does you the favor of moving your variable outside the loop where it belongs.


          Try code model generation tools at BoneSoft.com.

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #9

          :rose:

          Luc Pattyn


          try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


          1 Reply Last reply
          0
          • S Shy Agam

            Hello experts, There's something that has been bothering me for quite some time now... Is there a performance difference between the following two implementations?

            for (...)
            {
                int x = ...;
                // Do some stuff
            }
            
            int x;
            
            for (...)
            {
                // Do same stuff
            }
            

            In the first case, is x getting allocated for every step of the loop? Or does .NET knows to allocate it once, and use it again and again? Thanks in advance, Shy.

            S Offline
            S Offline
            snorkie
            wrote on last edited by
            #10

            For what its worth, I just ran a test and got the following results. It looks like it doesn't matter for something this small. Inside the loop took 552645639 ticks. Outside the loop took 549520699 ticks. The test code: long timeIn = 0; long timeOut = 0; long l = System.DateTime.Now.Ticks; for (long x = 0; x < 10000000000; x++) { int i = 1; i++; } timeIn = System.DateTime.Now.Ticks - l; l = System.DateTime.Now.Ticks; int i2; for (long x = 0; x < 10000000000; x++) { i2 = 1; i2++; } timeOut = System.DateTime.Now.Ticks - l; MessageBox.Show("Time In: " + timeIn.ToString() + Environment.NewLine + "Time Out: " + timeOut.ToString()); Hogan

            P L 2 Replies Last reply
            0
            • S snorkie

              For what its worth, I just ran a test and got the following results. It looks like it doesn't matter for something this small. Inside the loop took 552645639 ticks. Outside the loop took 549520699 ticks. The test code: long timeIn = 0; long timeOut = 0; long l = System.DateTime.Now.Ticks; for (long x = 0; x < 10000000000; x++) { int i = 1; i++; } timeIn = System.DateTime.Now.Ticks - l; l = System.DateTime.Now.Ticks; int i2; for (long x = 0; x < 10000000000; x++) { i2 = 1; i2++; } timeOut = System.DateTime.Now.Ticks - l; MessageBox.Show("Time In: " + timeIn.ToString() + Environment.NewLine + "Time Out: " + timeOut.ToString()); Hogan

              P Online
              P Online
              PIEBALDconsult
              wrote on last edited by
              #11

              Indeed, I found the same thing last week.

              1 Reply Last reply
              0
              • S snorkie

                For what its worth, I just ran a test and got the following results. It looks like it doesn't matter for something this small. Inside the loop took 552645639 ticks. Outside the loop took 549520699 ticks. The test code: long timeIn = 0; long timeOut = 0; long l = System.DateTime.Now.Ticks; for (long x = 0; x < 10000000000; x++) { int i = 1; i++; } timeIn = System.DateTime.Now.Ticks - l; l = System.DateTime.Now.Ticks; int i2; for (long x = 0; x < 10000000000; x++) { i2 = 1; i2++; } timeOut = System.DateTime.Now.Ticks - l; MessageBox.Show("Time In: " + timeIn.ToString() + Environment.NewLine + "Time Out: " + timeOut.ToString()); Hogan

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #12

                So the measured difference is less than 1% which is I guess well below your measurement accuracy: you are measuring elapsed time (good) over a long period (50 sec, good), but circumstances vary: there are hundreds of threads in an idle system, watching each and every part of your machine, there are disk transfers with DMA, there is a varying amount of Ethernet traffic including broadcast messages you are not interested in, etc etc. So my first impression is the execution times are the same. What you could do is reduce the loop count by a factor of 100, run it 100 times, and observe the distribution of the 100 timing results. The disappointment is neither the C# compiler nor the JIT compiler was smart enough to recognize the entire loop body could and should be moved outside, and then the loop is without subject; so it should have been hundred ticks at most ! On a non-CLR language (such as good old C) it takes quite some tricks to prevent the compiler from throwing it all out, and still get an accurate measurement ! :)

                Luc Pattyn


                try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


                1 Reply Last reply
                0
                • S Shy Agam

                  Hello experts, There's something that has been bothering me for quite some time now... Is there a performance difference between the following two implementations?

                  for (...)
                  {
                      int x = ...;
                      // Do some stuff
                  }
                  
                  int x;
                  
                  for (...)
                  {
                      // Do same stuff
                  }
                  

                  In the first case, is x getting allocated for every step of the loop? Or does .NET knows to allocate it once, and use it again and again? Thanks in advance, Shy.

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #13

                  Hi, This is related to the formatting, not the topic, of your message. I am preparing an article on copying code snippets from a CodeProject message board to Visual Studio; seems there are no problems when the browser used is FireFox, Safari, ... but often problems arise when using Internet Explorer. One of the factors is the tags that are present inside the < PRE> </PRE> block. In your message the lines are separated by <br> tags; most other messages have just regular newlines. Could you please tell me how you get that, i.e. what tools you use, and how you go about it. Thanks in advance.

                  Luc Pattyn


                  try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


                  S 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    Hi, This is related to the formatting, not the topic, of your message. I am preparing an article on copying code snippets from a CodeProject message board to Visual Studio; seems there are no problems when the browser used is FireFox, Safari, ... but often problems arise when using Internet Explorer. One of the factors is the tags that are present inside the < PRE> </PRE> block. In your message the lines are separated by <br> tags; most other messages have just regular newlines. Could you please tell me how you get that, i.e. what tools you use, and how you go about it. Thanks in advance.

                    Luc Pattyn


                    try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


                    S Offline
                    S Offline
                    Shy Agam
                    wrote on last edited by
                    #14

                    I'm not really sure if I understood what you mean, hence I don't know if this is the answer your after... I use Maxthon browser, and when inserting code snippets to my posts I wrap the <code></code> tags inside of the <pre></pre> tags, and not the other way around. If you could explain again, maybe I would be able to give you a better answer. :) Regards, Shy.

                    L 1 Reply Last reply
                    0
                    • S Shy Agam

                      I'm not really sure if I understood what you mean, hence I don't know if this is the answer your after... I use Maxthon browser, and when inserting code snippets to my posts I wrap the <code></code> tags inside of the <pre></pre> tags, and not the other way around. If you could explain again, maybe I would be able to give you a better answer. :) Regards, Shy.

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #15

                      Thanks, that is exactly what I wanted to know. I am not familiar with Maxthon browser, I never heard of it. The background is: if I select (part of) your code snippet using Internet Explorer, then copy it in Visual Studio, all formatting is lost, there are no line breaks any more. I created a utility that improves the situation in most cases; with it I manage to get newlines, and most of the time the indentation remains intact too; on your code snippet, with the tool, I do get newlines (so that is an improvement) but now I loose indentation. My article will suggest people use the PRE tags without adding CODE tags to it; could you please give that a try, so just reply to this message, and insert a code snippet as you usually do (that is using PRE tags) but this time without using CODE tags. Thanks in advance.

                      Luc Pattyn


                      try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


                      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