在foreach中使用大对象的最佳实践 [英] Best practice using large objects in foreach

查看:96
本文介绍了在foreach中使用大对象的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想讨论那些在foreach循环中使用大型对象的人们所感受到的最佳实践。对于

示例,如果您在这样的循环中重复使用Image对象(字母

以上片段仅供参考):


A

foreach(myList中的字符串)

{

图片img = Image.FromFile(s);

//操作img

}


它会更有效地运行吗?


B

图片img = null;

foreach(myList中的字符串s)

{

img = Image.FromFile (s);

//操作img

}





C

foreach(myList中的字符串s)

{

using(Image img = Image.FromFile(s);

{

//操作img

}

}


列表继续....


让我知道你的想法是什么。

I just wanted to throw the discussion out there on what the best
practice people feel is for using large objects in a foreach loop. For
example if you are reusing an Image object in a loop like this (letters
above snippets are for reference purposes):

A
foreach ( string s in myList )
{
Image img = Image.FromFile( s );
// operate on img
}

would it run more efficiently like this?

B
Image img = null;
foreach ( string s in myList )
{
img = Image.FromFile( s );
// operate on img
}

or

C
foreach ( string s in myList )
{
using ( Image img = Image.FromFile( s );
{
// operate on img
}
}

the list goes on....

Let me know what your thoughts are.

推荐答案

在任何情况下你都没有重新使用对象;你只是重新使用

变量,它只是(有效)一个整数,所以没有实际大小。

实际上,嵌套可能会被编译器规范化,所以没有

A和B之间真正的区别。即使你可以img.LoadFrom(即

使用相同的对象),它可能会在内部分配一个新的内存块

,所以你不会不要再使用大的记忆的一部分。


处理一次性物品对于这样的循环是一件好事,所以C

是好的。这样做;-p


Marc

In none of the cases are you re-using the object; you are just re-using
the variable, which is just (effectively) an integer, so no real size.
In fact, the nesting is likely to be normalized by the compiler, so no
real difference between A and B. Even if you could img.LoadFrom (i.e.
using the same object) it would likely assign a new memory block
internally, so you wouldn''t re-use the "big" portion of memory.

Disposing a disposable object is a good thing for loops like this, so C
is good. Do this ;-p

Marc


所以通过使用A和B,没有任何东西自动处理GC一次

它会在循环结束时触及吗?


Marc Gravell写道:
So by using A and B, nothing is disposed automatically by the GC once
it hits the end of the loop?

Marc Gravell wrote:

在任何情况下,您都不会重复使用该对象;你只是重新使用

变量,它只是(有效)一个整数,所以没有真正的大小。

实际上,嵌套可能会被归一化编译器,所以A和B之间没有真正的区别。即使你可以img.LoadFrom(即

使用相同的对象),它可能会分配一个新的内存块

内部,所以你不会再使用大的记忆的一部分。


处理一次性物品对于这样的循环是一件好事,所以C

是好的。这样做;-p

Marc
In none of the cases are you re-using the object; you are just re-using
the variable, which is just (effectively) an integer, so no real size.
In fact, the nesting is likely to be normalized by the compiler, so no
real difference between A and B. Even if you could img.LoadFrom (i.e.
using the same object) it would likely assign a new memory block
internally, so you wouldn''t re-use the "big" portion of memory.

Disposing a disposable object is a good thing for loops like this, so C
is good. Do this ;-p

Marc


我看不到任何对象重用,最多变量重用。编译器不关心

。 CLR也不是。

从绩效的角度来看,A& B是一样的。 C比较慢,因为

使用()。


Benny < be *********** @ gmail.comha scritto nel messaggio

news:11 ****************** ****@75g2000cwc.googlegro ups.com ...
I don''t see any object reuse, at most variable reuse. Compiler does not care
much about it. Nor does CLR.
From the performance point of view, A & B are the same. C is slower because
of using().

"Benny" <be***********@gmail.comha scritto nel messaggio
news:11**********************@75g2000cwc.googlegro ups.com...

>我只是想在那里讨论什么是最好的

练习人们认为在foreach循环中使用大对象。对于

示例,如果您在这样的循环中重复使用Image对象(字母

以上片段仅供参考):


A

foreach(myList中的字符串)

{

图片img = Image.FromFile(s);

//操作img

}


它会更有效地运行吗?


B

图片img = null;

foreach(myList中的字符串s)

{

img = Image.FromFile (s);

//操作img

}





C

foreach(myList中的字符串s)

{

using(Image img = Image.FromFile(s);

{

//操作img

}

}


列表继续....


让我知道你的想法。
>I just wanted to throw the discussion out there on what the best
practice people feel is for using large objects in a foreach loop. For
example if you are reusing an Image object in a loop like this (letters
above snippets are for reference purposes):

A
foreach ( string s in myList )
{
Image img = Image.FromFile( s );
// operate on img
}

would it run more efficiently like this?

B
Image img = null;
foreach ( string s in myList )
{
img = Image.FromFile( s );
// operate on img
}

or

C
foreach ( string s in myList )
{
using ( Image img = Image.FromFile( s );
{
// operate on img
}
}

the list goes on....

Let me know what your thoughts are.



这篇关于在foreach中使用大对象的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆