What is happening differently in the background for these two code blocks? Would one be considered "better" than the other?
My thought is that Example2 might be worse because it might have to wait for the garbage collector to dispose of the item, but I don't know enough about the garbage collector to know if that is true.
Example1:
ListItem item;
for (int i = 1; i <= 32; i++)
{
item = new ListItem();
//do some stuff
}
Example2:
for (int i = 1; i <= 32; i++)
{
ListItem item = new ListItem();
//do some stuff
}
Ecological overshoot occurs when humanity's demand on nature exceeds what ecosystems can supply. In other words, when we use more natural resources than the biosphere can regenerate.
Natural resources are used to make food, fuel and raw materials for the production of goods. All of the food that people eat comes from plants or animals. Natural resources such as coal, natural gas and oil provide heat, light and power.
Common examples of natural resources include air, sunlight, water, soil, stone, plants, animals and fossil fuels.
The world is using up more and more resources and global recycling is falling. That's the grim takeaway from a new report by the Circle Economy think tank, which found that the world used up more than 110 billion tons, or 100.6 billion metric tons, of natural resources, as Agence France-Presse (AFP) reported.
I have copied your code into Visual Studio, compiled it, and then looked at the generated IL. This is the IL generated from Example 1:
.method private hidebysig static void One() cil managed
{
// Code size 30 (0x1e)
.maxstack 2
.locals init ([0] class WinTest.ListItem item,
[1] int32 i,
[2] bool CS$4$0000)
IL_0000: nop
IL_0001: ldc.i4.1
IL_0002: stloc.1
IL_0003: br.s IL_0011
IL_0005: nop
IL_0006: newobj instance void WinTest.ListItem::.ctor()
IL_000b: stloc.0
IL_000c: nop
IL_000d: ldloc.1
IL_000e: ldc.i4.1
IL_000f: add
IL_0010: stloc.1
IL_0011: ldloc.1
IL_0012: ldc.i4.s 32
IL_0014: cgt
IL_0016: ldc.i4.0
IL_0017: ceq
IL_0019: stloc.2
IL_001a: ldloc.2
IL_001b: brtrue.s IL_0005
IL_001d: ret
} // end of method Program::One
And this is the IL generated from Example 2:
.method private hidebysig static void Two() cil managed
{
// Code size 30 (0x1e)
.maxstack 2
.locals init ([0] int32 i,
[1] class WinTest.ListItem item,
[2] bool CS$4$0000)
IL_0000: nop
IL_0001: ldc.i4.1
IL_0002: stloc.0
IL_0003: br.s IL_0011
IL_0005: nop
IL_0006: newobj instance void WinTest.ListItem::.ctor()
IL_000b: stloc.1
IL_000c: nop
IL_000d: ldloc.0
IL_000e: ldc.i4.1
IL_000f: add
IL_0010: stloc.0
IL_0011: ldloc.0
IL_0012: ldc.i4.s 32
IL_0014: cgt
IL_0016: ldc.i4.0
IL_0017: ceq
IL_0019: stloc.2
IL_001a: ldloc.2
IL_001b: brtrue.s IL_0005
IL_001d: ret
} // end of method Program::Two
As far as I understand, they are identical except for the fact that locals are declared (and thus accessed) in reverse order. I don't expect that to have any impact on performance whatsoever.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With