难道其中一个使用更多的资源比其他? [英] Does one of these use more resources than the other?
问题描述
什么是发生在不同的背景下,这两个code块?总会有被认为是好,比其他的?
我的想法是,例题可能会更糟,因为它可能必须等待垃圾回收处置项目,但我不知道有足够的了解垃圾收集知道,如果这是真的。
例1:
列表项的项目;
的for(int i = 1; I< = 32;我++)
{
项目=新的ListItem();
//做一些东西
}
例2:
的for(int i = 1; I< = 32;我++)
{
列表项的项目=新的ListItem();
//做一些东西
}
我抄你的code到Visual Studio,编译它,然后看了看产生IL。这是与IL从实施例1中产生:
。方法私人hidebysig静态无效一()CIL管理
{
// code尺寸30(0X1E)
.maxstack 2
.locals的init([0]类WinTest.ListItem项目,
[1] INT32我,
[2]布尔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实例无效WinTest.ListItem ::构造函数()
IL_000b:stloc.0
IL_000c:NOP
IL_000d:ldloc.1
IL_000e:ldc.i4.1
IL_000f:加
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
法计划} //结束::一
和,这是与IL从实施例2中产生:
。方法私人hidebysig静态无效的两个()CIL管理
{
// code尺寸30(0X1E)
.maxstack 2
.locals的init([0] INT32我,
[1]类WinTest.ListItem项目,
[2]布尔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实例无效WinTest.ListItem ::构造函数()
IL_000b:stloc.1
IL_000c:NOP
IL_000d:ldloc.0
IL_000e:ldc.i4.1
IL_000f:加
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
法计划} //结束::两
据我了解,他们除了以相反的顺序的事实,当地人声明(因此访问)相同。我没有想到的是对性能承担任何影响。
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
}
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.
这篇关于难道其中一个使用更多的资源比其他?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!