When should I create another instance?

If I have a loop and I have to use another object each time I iterate. Should I instantiate this object for each step, or should I instantiate it outside the loop By doing it outside the loop I might have some problems concerning getting "ghost" information from the previous iteration. But, by doing it inside the loop (for each step) I would slow down the whole algorithm, besides consuming more memory...

Any opinion on this




Answer this question

When should I create another instance?

  • Lennie Oxyer

    OmegaMan wrote:
    CanUHelpMe wrote:

    Hi,

    I didn't got the answer yet.

    if i am using c# 2.0, should we instantiate the variable inside or outside the loop.

    my major concerns are performance(highest priority) and less memory usage(middle priority)

    regards'



    Without the knowledge of what the object is and what the object does...how is one supposed to be able to determine what is the best course in a forum post This is true for any language not just .Net. such as in C/C++/Java/Ada...I could go on.

    If you have the luxury of doing both operations, outside and inside the loop..then setup two different programs and monitor the time it takes as well as monitor the program resource footprint using perfmon.

    From those tests you will be able to answer the question based on those two data points.

    I'll second OmegaMan's opinion that it depends on your specific condition.

    With that said in C# 2.0 the following two code fragments will emit the same IL from which we can draw a conclusion that there is no performance or memory hit declaring the variable inside the loop. Because of this I declare variable inside the loop to eliminate any possibility of the mistake and also for added clarity.

    string[] collection = {"0", "1", "2", "3"};

    for (int index = 0; index < collection.Length; index++)

    {

    string value = collection[index];

    }

    string[] collection = {"0", "1", "2", "3"};

    string newValue;

    for (int index2 = 0; index2 < collection.Length; index2++)

    {

    newValue = collection[index2];

    }


  • xr280xr

    Could you instead take what is needed within the loop and expose that as a static method on the other object That way it is stateless by design and the creation of an object is not required.


  • charles C

    CanUHelpMe wrote:

    Hi,

    I didn't got the answer yet.

    if i am using c# 2.0, should we instantiate the variable inside or outside the loop.

    my major concerns are performance(highest priority) and less memory usage(middle priority)

    regards'



    Without the knowledge of what the object is and what the object does...how is one supposed to be able to determine what is the best course in a forum post This is true for any language not just .Net. such as in C/C++/Java/Ada...I could go on.

    If you have the luxury of doing both operations, outside and inside the loop..then setup two different programs and monitor the time it takes as well as monitor the program resource footprint using perfmon.

    From those tests you will be able to answer the question based on those two data points.



  • LalitBoliya

    Hi,

    I think the answer to your question depends on two factors: what language you're using and what are your priorities.

    The language you're using will influence how much of the performance hit you're tkaing by defining the variable inside the loop. In my opinion (and I remember this is what C# 2.0 compiler does) compiler should optimize away the variable - that is put it outside the definition of the loop. My approach in C# was tu go for clarity and rely on the compiler. If you're very concerned about the performance you could check the IL/assembly code emitted by the compiler using a tool like Reflector or ILDASM.

    Hope this helps.


  • Ruprect8696

    Hi,

    I didn't got the answer yet.

    if i am using c# 2.0, should we instantiate the variable inside or outside the loop.

    my major concerns are performance(highest priority) and less memory usage(middle priority)

    regards'


  • When should I create another instance?