问题描述
属性预期性能类似的字段,即使它们真的功能。什么可以说对任意业绩预期的IEnumerable
?
Properties are expected to perform similarly to fields, even though they are really functions. What can be said about the expected performance of an arbitrary IEnumerable
?
它是公平的,从性能借用的概念,并说,一个的IEnumerable
应该执行大致相同迭代数组或名单,其中,T>
?
Is it fair to borrow the concept from properties and say that an IEnumerable
should perform about the same as iterating an array or List<T>
?
或者是它没关系几乎任何发生在每次迭代:数据库访问,Web服务调用,耗时的计算等。
Or, is it okay for just about anything happen with each iteration: database access, web service call, time-consuming computation, etc.
推荐答案
在谈到从经验,你真的不能推断出任何关于任意的IEnumerable
的性能。例如,它可能是一个的IQueryable
的化身,每次枚举时命中数据库。或者,它可能的结果 File.EnumerateLines
。
Speaking from experience you can't really infer anything about the performance of an arbitrary IEnumerable
. For example, it might be an IQueryable
in disguise, hitting the database every time it is enumerated. Or it might be the result of File.EnumerateLines
.
有时候,重要的是可枚举只能用一次列举
Sometimes it is important that the enumerable only be enumerated once.
这是相对于一个属性。如果一个属性是访问数据库或读我档案我会认为这是一个code气味。对于的IEnumerable
这只是正常的。
This is in contrast to a property. If a property was to hit the database or read I file I'd consider this to be a code smell. For an IEnumerable
it is just normal.
这篇关于什么是IEnumerable的预期的性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!