(1)
Shape shape1 = null;
for (int i = 0; i < 100000000; i++)
{
index = random.Next(3);
shape1 = Shape.CreateShape(ran[index]);
totalArea += shape1.GetArea(); }
(2)
for (int i = 0; i < 100000000; i++)
{
index = random.Next(3);
Shape shape1 = Shape.CreateShape(ran[index]);
totalArea += shape1.GetArea(); }
2、在第一段代码里,shape1的地址只有一个。
3、在第二段代码里,你创建了 100000000 shape1 对象。个人理解,第一段代码优先考虑。
仅供参考。期待
具体的差别可能要看Shape 对象都需要什么资源
因为shape对象创建了100000000,就需要更多的内存分配
都是创建100000000个Shape对象