Declaring variables in loops

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?QUw=?=

    Declaring variables in loops

    Hi

    I usually stick to the convention of not declaring variables in my bodies of
    "loops" (including foreach)

    ie

    int x;

    for (int i = 0; i < 10; i++) {
    x = ...
    }

    as opposed to
    for (int i = 0; i < 10; i++) {
    int x = ...
    }

    I've heard that in Java declaring variables within loops can cause problems
    and I've tried to create some metrics to compare the difference in C# code
    between prior to the loop and within the loop.

    My results indicated that there was no difference.

    I'd like to get a better idea from any one else as to whether my conclusion
    is acurate or there is some form of performance hit.

    Thanks in advance

    AL

  • Mike J

    #2
    Re: Declaring variables in loops

    below is a smple of how i approach loops..im not a c# guru..but looping to
    me crating a new class each time would be a preformance hit
    versus create the class once and change its existing value....(no mem clean
    up or construction of a new class)

    int x=0;
    int y=100;
    int z=1000;

    for (x=0;x<y;x++)
    {
    z+=x;
    Console.WriteLi ne(z.ToString() );
    }
    Console.ReadKey ();

    MJ

    "AL" <AL@discussions .microsoft.comw rote in message
    news:38468A9E-2697-42C2-A254-008F3709D724@mi crosoft.com...
    Hi
    >
    I usually stick to the convention of not declaring variables in my bodies
    of
    "loops" (including foreach)
    >
    ie
    >
    int x;
    >
    for (int i = 0; i < 10; i++) {
    x = ...
    }
    >
    as opposed to
    for (int i = 0; i < 10; i++) {
    int x = ...
    }
    >
    I've heard that in Java declaring variables within loops can cause
    problems
    and I've tried to create some metrics to compare the difference in C# code
    between prior to the loop and within the loop.
    >
    My results indicated that there was no difference.
    >
    I'd like to get a better idea from any one else as to whether my
    conclusion
    is acurate or there is some form of performance hit.
    >
    Thanks in advance
    >
    AL
    >

    Comment

    • Mattias Sjögren

      #3
      Re: Declaring variables in loops

      >I'd like to get a better idea from any one else as to whether my conclusion
      >is acurate or there is some form of performance hit.
      Performance wise there shouldn't be any difference (as long as you
      don't also initialize the variable in the declaration).

      Personally I prefer to declare the variable inside the loop, and in
      general to keep the variable scope as narrow as possible. That
      prevents me from accidentally using a variable after I'm done with it.


      Mattias

      --
      Mattias Sjögren [C# MVP] mattias @ mvps.org
      http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
      Please reply only to the newsgroup.

      Comment

      • Tom Porterfield

        #4
        Re: Declaring variables in loops

        AL wrote:
        Hi
        >
        I usually stick to the convention of not declaring variables in my bodies of
        "loops" (including foreach)
        >
        ie
        >
        int x;
        >
        for (int i = 0; i < 10; i++) {
        x = ...
        }
        >
        as opposed to
        for (int i = 0; i < 10; i++) {
        int x = ...
        }
        >
        I've heard that in Java declaring variables within loops can cause problems
        and I've tried to create some metrics to compare the difference in C# code
        between prior to the loop and within the loop.
        >
        My results indicated that there was no difference.
        >
        I'd like to get a better idea from any one else as to whether my conclusion
        is acurate or there is some form of performance hit.
        >
        Thanks in advance
        In such a simple scenario as this the IL that's generated is the same so
        the performance should also be the same.
        --
        Tom Porterfield

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: Declaring variables in loops

          AL <AL@discussions .microsoft.comw rote:
          I usually stick to the convention of not declaring variables in my bodies of
          "loops" (including foreach)
          >
          ie
          >
          int x;
          >
          for (int i = 0; i < 10; i++) {
          x = ...
          }
          >
          as opposed to
          for (int i = 0; i < 10; i++) {
          int x = ...
          }
          No, the latter is generally nicer - it avoids "polluting" the namespace
          of available variables, and shows that you genuinely intend to only use
          the variable within the loop.
          I've heard that in Java declaring variables within loops can cause problems
          Until I see any evidence of of it, I'd be very sceptical of that.
          Certainly in the all the time I've written Java, I've never run into
          any issues like that.
          and I've tried to create some metrics to compare the difference in C# code
          between prior to the loop and within the loop.
          >
          My results indicated that there was no difference.
          >
          I'd like to get a better idea from any one else as to whether my conclusion
          is acurate or there is some form of performance hit.
          No, there isn't a performance hit unless you're able to initialize the
          variable once and then leave it initialized to the same value. Even
          then I'd take the readability improvement of the latter style over the
          *possible* slight performance improvement of the former until I'd
          proved it was a bottleneck.

          --
          Jon Skeet - <skeet@pobox.co m>
          http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
          If replying to the group, please do not mail me too

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Declaring variables in loops

            Mike J <vettes_n_jets@ sbcglobal.netwr ote:
            below is a smple of how i approach loops..im not a c# guru..but looping to
            me crating a new class each time would be a preformance hit
            versus create the class once and change its existing value....(no mem clean
            up or construction of a new class)
            >
            int x=0;
            int y=100;
            int z=1000;
            >
            for (x=0;x<y;x++)
            {
            z+=x;
            Console.WriteLi ne(z.ToString() );
            }
            Console.ReadKey ();
            Other than z.ToString() which is called on every iteration of the loop,
            where do you see objects being created in the above? It's much neater
            IMO to do

            // These need to be outside the loop anyway
            int y = 100;
            int z = 1000;

            for (int x=0; x < y; x++)
            {
            z += x;
            Console.WriteLi ne(z.ToString() );
            }

            Bottom line - don't trust instinct about performance without proof,
            especially if it affects readability. (In your code it isn't clear that
            x is only interesting within the loop - in my code it is.)

            --
            Jon Skeet - <skeet@pobox.co m>
            http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
            If replying to the group, please do not mail me too

            Comment

            • Ignacio Machin \( .NET/ C# MVP \)

              #7
              Re: Declaring variables in loops

              Hi,

              "Mike J" <vettes_n_jets@ sbcglobal.netwr ote in message
              news:vC%4i.6173 $4Y.2606@newssv r19.news.prodig y.net...
              below is a smple of how i approach loops..im not a c# guru..but looping to
              me crating a new class each time would be a preformance hit
              versus create the class once and change its existing value....(no mem
              clean up or construction of a new class)
              I do not understand your example.

              Declaring a new instance inside the loop sometime makes sense, some other
              times it does not.

              if it's a valued type you incurr in no memory overhead at all. (see other;s
              post in this thread). With a reference type it might have an impact.


              Comment

              Working...