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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Which solution is of more performance ? Instantiating new object within a for loop

Which solution is of more performance ? Instantiating new object within a for loop

Scheduled Pinned Locked Moved C#
performancehelpquestion
7 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.
  • N Offline
    N Offline
    Nadia Monalisa
    wrote on last edited by
    #1

    hello, I am confused about choosing a solution between two options and I am not sure which one is of more performance, would you please help me !! Solution A ============

    myClass A;

    for(int i=0; i < aNumber; i++)
    {
    A = new myClass(i);

    ......................
    }

    Solution B =============

    for(int i=0; i < aNumber; i++)
    {
    myClass A = new myClass(i);

    ......................
    }

    P L C P T 5 Replies Last reply
    0
    • N Nadia Monalisa

      hello, I am confused about choosing a solution between two options and I am not sure which one is of more performance, would you please help me !! Solution A ============

      myClass A;

      for(int i=0; i < aNumber; i++)
      {
      A = new myClass(i);

      ......................
      }

      Solution B =============

      for(int i=0; i < aNumber; i++)
      {
      myClass A = new myClass(i);

      ......................
      }

      P Offline
      P Offline
      Patrick_N3WB1E
      wrote on last edited by
      #2

      The solution A is looking like my favorite, its reduced. For one thing whats it for that might change my mind :)

      1 Reply Last reply
      0
      • N Nadia Monalisa

        hello, I am confused about choosing a solution between two options and I am not sure which one is of more performance, would you please help me !! Solution A ============

        myClass A;

        for(int i=0; i < aNumber; i++)
        {
        A = new myClass(i);

        ......................
        }

        Solution B =============

        for(int i=0; i < aNumber; i++)
        {
        myClass A = new myClass(i);

        ......................
        }

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

        Hi, your two code snippets will compile to the same IL instructions and run identically. The only difference is in the scope of A; in solution A, A still has a value when the for loop exits (assuming aNumber is greater than zero); whereas in B, A is dead and burried when the loop exits. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        Fixturized forever. :confused:


        modified on Thursday, December 11, 2008 7:46 PM

        1 Reply Last reply
        0
        • N Nadia Monalisa

          hello, I am confused about choosing a solution between two options and I am not sure which one is of more performance, would you please help me !! Solution A ============

          myClass A;

          for(int i=0; i < aNumber; i++)
          {
          A = new myClass(i);

          ......................
          }

          Solution B =============

          for(int i=0; i < aNumber; i++)
          {
          myClass A = new myClass(i);

          ......................
          }

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          The second is better, it's more readable by far. It also limits the scope of the object, and if you don't need it outside the loop, that's a good thing.

          Christian Graus Driven to the arms of OSX by Vista.

          1 Reply Last reply
          0
          • N Nadia Monalisa

            hello, I am confused about choosing a solution between two options and I am not sure which one is of more performance, would you please help me !! Solution A ============

            myClass A;

            for(int i=0; i < aNumber; i++)
            {
            A = new myClass(i);

            ......................
            }

            Solution B =============

            for(int i=0; i < aNumber; i++)
            {
            myClass A = new myClass(i);

            ......................
            }

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            Yeah, what they said, except B always looks like it'll waste cycles.

            1 Reply Last reply
            0
            • N Nadia Monalisa

              hello, I am confused about choosing a solution between two options and I am not sure which one is of more performance, would you please help me !! Solution A ============

              myClass A;

              for(int i=0; i < aNumber; i++)
              {
              A = new myClass(i);

              ......................
              }

              Solution B =============

              for(int i=0; i < aNumber; i++)
              {
              myClass A = new myClass(i);

              ......................
              }

              T Offline
              T Offline
              Tony Pottier
              wrote on last edited by
              #6

              If you just need to run some stuff in A, then throw it away, maybe you should consider adding some kind of "reset" method to myClass which will probably be much faster than instantiating a new object again and again. Just run a quick benchmark with

              DateTime start = DateTime.Now;
              for(int i=0; i < aNumber; i++)
              {
              myClass A = new myClass(i);

              ......................
              }
              DateTime end = DateTime.Now;
              MessageBox.Show( ((TimeSpan)(end-start)).TotalMilliseconds );

              P 1 Reply Last reply
              0
              • T Tony Pottier

                If you just need to run some stuff in A, then throw it away, maybe you should consider adding some kind of "reset" method to myClass which will probably be much faster than instantiating a new object again and again. Just run a quick benchmark with

                DateTime start = DateTime.Now;
                for(int i=0; i < aNumber; i++)
                {
                myClass A = new myClass(i);

                ......................
                }
                DateTime end = DateTime.Now;
                MessageBox.Show( ((TimeSpan)(end-start)).TotalMilliseconds );

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #7

                That depends on how expensive the instantiation is. A few months ago I spent some time working on a technique to keep using one static StringBuilder throughout a library of static methods; clearing it before each use rather than instantiating (and re-extending as necessary). In the case of StringBuilder, my technique wasn't worth the trouble. :( And for quick benchmarks like that I now use a System.Diagnostics.Stopwatch

                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