关于表现的另一个问题 [英] Another question about performance

查看:80
本文介绍了关于表现的另一个问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




考虑一下:


ArrayList al = new ArrayList();

FillList( al);

///现在两个场景:


/// 1.

for(int i = 0;我< al.Count; i ++)

{..无论如何..}


/// 2.

int iCount = al.Count;

for(int i = 0; i< iCount; i ++)

{..do whatever ..}

是的,因为你已经发现我的问题是关于优先的

场景。我记得形成Delphi Pascal的日子,你应该在第一个属性中使用
预先设置第二个场景。在每个循环中将要求获得


在C#和托管C ++中是否有任何变化,或者仍然提供使用

方案2?


谢谢

MuZZy

解决方案

MuZZy< tn*@newsgroups.nospam>写道:

考虑一下:

ArrayList al = new ArrayList();
FillList(al);
/// NOW TWO SCENARIOS:

/// 1.
for(int i = 0; i< al.Count; i ++)
{..do whatever ..}

/// 2.
int iCount = al.Count;
for(int i = 0; i< iCount; i ++)
{..do whatever ..}

是的,因为你已经发现我的问题是关于preffered
场景。我记得在Delphi Pascal的日子里,你应该像第一个属性那样预先设置第二个场景。每个循环都会请求Count.

C#中的任何内容都有变化并且管理好了C ++或它仍然提供使用
情景2?




1)无论如何,该物业几乎肯定会被内联

2)性能差异,即使有一个非常非常非常不可能造成任何赔率

3)可读性差异更为显着,IMO
4)你真的需要i,还是只需要元素?如果是后者,

请考虑使用foreach。


-

Jon Skeet - < sk * **@pobox.com>
http://www.pobox.com/ ~Sibet 博客: http://www.msmvps.com/jon。双向飞碟

如果回复小组,请不要给我发邮件


MuZZy写道:



考虑一下:

ArrayList al = new ArrayList();
FillList(al);
///现在两个场景:

/// 1.
for(int i = 0; i< al.Count; i ++)
{..do whatever ..}

/// 2.
int iCount = al.Count;
for(int i = 0; i< iCount; i ++)
{..do whatever ..}

是的,因为你已经发现我的问题是关于preffered
场景。我记得在Delphi Pascal的日子里,你应该像第一个属性那样预先设置第二个场景。每个循环都会请求Count.

C#中的任何内容都有变化并且管理好了C ++还是它仍然提供使用
场景2?

谢谢
MuZZy



它已经改变了,它是方案(1)当您使用索引器访问ArrayList中的项目时,
。通过使用al.Count将没有运行时

检查:-)

http://tinyurl.com/3e9n3 ,部分范围检查消除


此外,当你有一个IEnumerable(一个ArrayList是)和

IEnumerable是*不是* System.Array,这是好的:

foreach(在myList中的OBJECT o)

{

}

HTH,

Andy

-

发送电子邮件我直接删除下面的* NO * SPAM *部分:

*NO*SPAM*xmen40@*NO*SPAM*gmx.net


< blockquote> Jon Skeet [C#MVP]写道:

MuZZy< tn*@newsgroups.nospam>写道:

考虑一下:

ArrayList al = new ArrayList();
FillList(al);
/// NOW TWO SCENARIOS:

/// 1.
for(int i = 0; i< al.Count; i ++)
{..do whatever ..}

/// 2.
int iCount = al.Count;
for(int i = 0; i< iCount; i ++)
{..do whatever ..}

是的,因为你已经发现我的问题是关于preffered
场景。我记得在Delphi Pascal的日子里,你应该像第一个属性那样预先设置第二个场景。每个循环都会请求Count.

C#中的任何内容都有变化并且管理好了C ++还是它仍然可以使用
场景2?



1)无论如何,该属性几乎肯定会内联
2)性能差异,即使有一个是非常,非常,不可能做出任何可能性
3)可读性差异更为显着,IMO
4)你真的需要我,或只是元素?如果是后者,请考虑使用foreach。




感谢您的回复Jon!


让我们想象一下,我需要在循环中使用''我',或者它是C ++所以我不需要'b $ b'不能使用''foreach''。

现在,编译器内联属性如果不能保证

它不会在循环中改变?


Hi,

Consider this:

ArrayList al = new ArrayList();
FillList(al);
/// NOW TWO SCENARIOS:

/// 1.
for (int i = 0 ; i < al.Count ; i++)
{..do whatever..}

/// 2.
int iCount = al.Count;
for (int i = 0 ; i < iCount ; i++)
{..do whatever..}
Yes, as you have already figured out my question is about preffered
scenario. I remember form the days of Delphi Pascal that you should
preffer second scenario as in the first property Count will be requested
on every loop.

Did anything change in C# and managed C++ or it''s still proffered to use
scenario 2?

Thank you
MuZZy

解决方案

MuZZy <tn*@newsgroups.nospam> wrote:

Consider this:

ArrayList al = new ArrayList();
FillList(al);
/// NOW TWO SCENARIOS:

/// 1.
for (int i = 0 ; i < al.Count ; i++)
{..do whatever..}

/// 2.
int iCount = al.Count;
for (int i = 0 ; i < iCount ; i++)
{..do whatever..}
Yes, as you have already figured out my question is about preffered
scenario. I remember form the days of Delphi Pascal that you should
preffer second scenario as in the first property Count will be requested
on every loop.

Did anything change in C# and managed C++ or it''s still proffered to use
scenario 2?



1) The property will almost certainly be inlined anyway
2) The performance difference even if there is one is very, very
unlikely to make any odds
3) The readability difference is much more significant, IMO
4) Do you actually need "i", or just the element? If it''s the latter,
consider using foreach instead.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


MuZZy wrote:

Hi,

Consider this:

ArrayList al = new ArrayList();
FillList(al);
/// NOW TWO SCENARIOS:

/// 1.
for (int i = 0 ; i < al.Count ; i++)
{..do whatever..}

/// 2.
int iCount = al.Count;
for (int i = 0 ; i < iCount ; i++)
{..do whatever..}
Yes, as you have already figured out my question is about preffered
scenario. I remember form the days of Delphi Pascal that you should
preffer second scenario as in the first property Count will be requested
on every loop.

Did anything change in C# and managed C++ or it''s still proffered to use
scenario 2?

Thank you
MuZZy


It has changed, it is scenario (1) when you use the indexer to access
the items in the ArrayList. By using al.Count there will be no runtime
check :-)
see http://tinyurl.com/3e9n3, part "Range Check Elimination"

Also, when you have an IEnumerable (which an ArrayList is) and the
IEnumerable is *not* an System.Array, this is as good:
foreach(OBJECT o in myList)
{
}
HTH,
Andy
--
To email me directly, please remove the *NO*SPAM* parts below:
*NO*SPAM*xmen40@*NO*SPAM*gmx.net


Jon Skeet [C# MVP] wrote:

MuZZy <tn*@newsgroups.nospam> wrote:

Consider this:

ArrayList al = new ArrayList();
FillList(al);
/// NOW TWO SCENARIOS:

/// 1.
for (int i = 0 ; i < al.Count ; i++)
{..do whatever..}

/// 2.
int iCount = al.Count;
for (int i = 0 ; i < iCount ; i++)
{..do whatever..}
Yes, as you have already figured out my question is about preffered
scenario. I remember form the days of Delphi Pascal that you should
preffer second scenario as in the first property Count will be requested
on every loop.

Did anything change in C# and managed C++ or it''s still proffered to use
scenario 2?



1) The property will almost certainly be inlined anyway
2) The performance difference even if there is one is very, very
unlikely to make any odds
3) The readability difference is much more significant, IMO
4) Do you actually need "i", or just the element? If it''s the latter,
consider using foreach instead.



Thanks for your reply Jon!

Let''s imagine that i need ''i'' inside the loop, or that it''s C++ so i
can''t use ''foreach''.
Now, how does compiler inline property if there is no guarantee that
it''s not going to change inside the loop?


这篇关于关于表现的另一个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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