Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7872301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:09:47+00:00 2026-06-03T02:09:47+00:00

Possible Duplicate: Difference between declaring variables before or in loop? I always wondered, is

  • 0

Possible Duplicate:
Difference between declaring variables before or in loop?

I always wondered, is it faster to do like this:

int i;

for (i=0;i<...)
for (i=0;i<...)
for (i=0;i<...)
for (i=0;i<...)

or

for (int i=0;i<...)
for (int i=0;i<...)
for (int i=0;i<...)
for (int i=0;i<...)

Meaning, if i have multiple for loops in one function, does it work faster if i declare loop iteration variable once and use it multiple times, or declare it inside each for loop?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T02:09:49+00:00Added an answer on June 3, 2026 at 2:09 am

    The IL generated will be (for release build) pretty much identical for both approaches.

    Consider this code:

    static int test1()
    {
        int result = 0;
        int i;
    
        for (i = 0; i < 10; ++i)
            ++result;
    
        for (i = 0; i < 10; ++i)
            ++result;
    
        return result;
    }
    
    static int test2()
    {
        int result = 0;
    
        for (int i = 0; i < 10; ++i)
            ++result;
    
        for (int i = 0; i < 10; ++i)
            ++result;
    
        return result;
    }
    

    This generates the following IL for a release build, which I have placed side-by-side for easier comparison:

    test1():                        test2()
    {                               {
        .maxstack 2                     .maxstack 2
        .locals init (                  .locals init (
            [0] int32 result,               [0] int32 result,
            [1] int32 i)                    [1] int32 i,
                                            [2] int32 V_2)
        L_0000: ldc.i4.0                L_0000: ldc.i4.0 
        L_0001: stloc.0                 L_0001: stloc.0 
        L_0002: ldc.i4.0                L_0002: ldc.i4.0 
        L_0003: stloc.1                 L_0003: stloc.1 
        L_0004: br.s L_000e             L_0004: br.s L_000e
        L_0006: ldloc.0                 L_0006: ldloc.0 
        L_0007: ldc.i4.1                L_0007: ldc.i4.1 
        L_0008: add                     L_0008: add 
        L_0009: stloc.0                 L_0009: stloc.0 
        L_000a: ldloc.1                 L_000a: ldloc.1 
        L_000b: ldc.i4.1                L_000b: ldc.i4.1 
        L_000c: add                     L_000c: add 
        L_000d: stloc.1                 L_000d: stloc.1 
        L_000e: ldloc.1                 L_000e: ldloc.1 
        L_000f: ldc.i4.s 10             L_000f: ldc.i4.s 10
        L_0011: blt.s L_0006            L_0011: blt.s L_0006
        L_0013: ldc.i4.0                L_0013: ldc.i4.0 
        L_0014: stloc.1                 L_0014: stloc.2 
        L_0015: br.s L_001f             L_0015: br.s L_001f
        L_0017: ldloc.0                 L_0017: ldloc.0 
        L_0018: ldc.i4.1                L_0018: ldc.i4.1 
        L_0019: add                     L_0019: add 
        L_001a: stloc.0                 L_001a: stloc.0 
        L_001b: ldloc.1                 L_001b: ldloc.2 
        L_001c: ldc.i4.1                L_001c: ldc.i4.1 
        L_001d: add                     L_001d: add 
        L_001e: stloc.1                 L_001e: stloc.2 
        L_001f: ldloc.1                 L_001f: ldloc.2 
        L_0020: ldc.i4.s 10             L_0020: ldc.i4.s 10
        L_0022: blt.s L_0017            L_0022: blt.s L_0017
        L_0024: ldloc.0                 L_0024: ldloc.0 
        L_0025: ret                     L_0025: ret 
    }                               }
    

    This makes it pretty clear that you should choose the version where ‘i’ is local to the loop, because that’s better practice.

    However, the version with the loop counter declared outside the loops will be faster by the amount of time needed to initialise an int to zero – pretty much negligible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Difference between declaring variables before or in loop? Consider the two codes
Possible Duplicate: Difference between declaring variables before or in loop? Is there any (or
Possible Duplicate: Difference between int[] array and int array[] I was sure that this
Possible Duplicate: Difference between void main and int main? Why is void main() {
Possible Duplicate: What’s the difference between “Array()” and “[]” while declaring a JavaScript array?
Possible Duplicate: Difference between void main and int main? Alright, so I'm using bloodshed
Possible Duplicate: Difference between “var” and “object” in C# I would like to know
Possible Duplicate: Difference between i++ and ++i in a loop? Is there a performance
Possible Duplicate: difference between destructor and garbage collector Recently i asked this question ,
Possible Duplicate: Difference between a Structure and a Union in C I see this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.