Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# what is faster, declare loop variable at the beginning of the function, or inside each for loop? [duplicate]

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?

like image 727
Istrebitel Avatar asked Feb 20 '23 16:02

Istrebitel


2 Answers

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.

like image 160
Matthew Watson Avatar answered Apr 26 '23 22:04

Matthew Watson


In theory the former should be faster, because there is only one memory location, which is reused.

like image 40
Petar Ivanov Avatar answered Apr 26 '23 23:04

Petar Ivanov