简体   繁体   English

有关枚举空列表的性能问题

[英]Performance question about enumerating empty lists

which is better, performance-wise, when you potentially have an empty list? 当您可能有一个空列表时,从性能角度来看哪个更好?

if (_myList != null && _myList.Count > 0)
{
  foreach (thing t in _myList )
  {
    ...

or without checking if _myList count contains anything: 或不检查_myList计数是否包含任何内容:

if (_myList != null)
{
  foreach (thing t in _myList )
  {
   ...

I'm guessing there's probably not much in it, but that the first one is slightly quicker (?) 我猜有可能是在它不多,但第一个是快(?)

Thanks 谢谢


edit: 编辑:

To clarify, I mean a list like this: List<Thing> 为了澄清,我的意思是这样的列表: List<Thing>

There is only one way to answer a performance question: 回答性能问题只有一种方法:

  1. Measure 测量
  2. Measure 测量
  3. Measure 测量

The only way you can know if: 您知道的唯一方法是:

  • I can improve the code 我可以改善代码
  • I have improved the code 已经改善了代码
  • And most importantly: What code to improve first 最重要的是: 首先要改进哪些代码

is to measure how much time different parts of the program is taking, and then improving the top items first. 是衡量计划的不同部分花费了多少时间,然后首先改善了主要项目。

To answer your question, I would assume that the miniscule overhead of a few extra objects is indeed going to cost you some cycles compared to just calling Count (assuming that is a fast call, field read for instance). 要回答您的问题,我假设与仅调用Count(假设这是一个快速调用,例如读取字段)相比,一些额外对象的微小开销确实会花费一些周期。

However, since you're asking this question it tells me that you don't have enough information about the state of your program and your code, so the chance of improving that miniscule overhead actually having a noticable effect for your users is so slim I wouldn't bother. 但是,由于您在问这个问题,它告诉我您没有有关程序状态和代码状态的足够信息,因此,改善这种微不足道的开销实际上对用户产生显着影响的机会非常渺茫,不会打扰。

I can guarantee you have bigger fish to fry performance-wise, so tackle those first. 我可以保证您有更大的鱼可以按照性能进行油炸,因此请先解决这些问题。

Personally I don't use null references except when dealing with databases or in a few lines of code to signal "not initialized yet", other than that I use empty lists and strings, etc. Your code is much easier to read and understand, and the benefit of microoptimization on this level will never be noticed. 我个人不使用null引用,除非在处理数据库或使用几行代码来表示“尚未初始化”时使用,否则我使用空列表和字符串等。您的代码更易于阅读和理解,并且永远不会注意到在此级别进行微优化的好处。

Unless you are calling your code in a tight loop, the difference will be insignificant. 除非您在紧密的循环中调用代码,否则差异将很小。 However, be advised that there is a difference: the check for _myList.Count > 0 avoids the calling of GetEnumerator , the creation of an IEnumerator implementing object (a heap allocation) and a call to that enumerator's MoveNext() method. 但是,请注意有区别: _myList.Count > 0的检查避免了GetEnumerator的调用,IEnumerator实现对象的创建(堆分配)以及对该枚举器的MoveNext()方法的MoveNext()

If you are in a tight spot performance-wise that avoided (heap allocation + virtual method calls) might help, but in general your code is shorter and easier to understand by avoiding the explicit on _myList.Count . 如果您在性能上处于紧要关头,那么避免(堆分配+虚拟方法调用)可能会有所帮助,但是通常,通过避免在_myList.Count上使用显式,可以使代码更短,更易于理解。

Compulsory Disclaimer: You should have already identified this as a problem area via profiling before attempting to "optimise it", and hence you'll already have a the tools at hand to determine quickly and easily which methods faster. 强制性免责声明:在尝试“优化”之前,您应该已经通过概要分析将其识别为问题区域,因此,您将拥有一个快速便捷地确定哪些方法更快的工具。 Odds are, neither will make an appreciable difference to your application's performance. 很有可能,这都不会对您的应用程序的性能产生明显的影响。

But that being said, Count, for System.Generics.Collection.List<> will almost definitely be quicker. 话虽这么说,Count,对于System.Generics.Collection.List <>几乎肯定会更快。

Although optimisation improves things greatly (don't be scared of using foreach ! it's nearly free), foreach more or less involves: 尽管优化极大地改善了性能(不用担心使用foreach !它几乎是免费的),但是foreach或多或少涉及:

var enumerator = _myList.GetEnumerator();
try
{
  while (enumerator.MoveNext())
  {
  }
}
finally
{
  enumerator.Dispose();
}

which is a lot more complicated than merely comparing a simple property (safe assumption that List.Count is a simple property) with a constant. 这比仅将简单属性(安全假设List.Count是简单属性)与常量进行比较要复杂得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM