简体   繁体   中英

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:

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>

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).

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.

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.

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 .

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.

Although optimisation improves things greatly (don't be scared of using foreach ! it's nearly free), foreach more or less involves:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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