I often write code like this:
if ( list.Count > 0 ) { }
Is this efficient? Does this operation look like:
Or like this:
That is, to get the number of elements in the list, do you have to count all the way through the list, or is the number of elements recorded somewhere? And is this the case for all ICollection
classes?
What about the Capacity
of the list?
The simple answer is no – there is no difference at all. The COUNT(*) function counts the total rows in the table, including the NULL values. The semantics for COUNT(1) differ slightly; we'll discuss them later. However, the results for COUNT(*) and COUNT(1) are identical.
As lithium-ion batteries chemically age, the amount of charge they can hold diminishes, resulting in shorter amounts of time before a device needs to be recharged. This can be referred to as the battery's maximum capacity—the measure of battery capacity relative to when it was new.
Because all data (including Row Data) is stored in B-Tree indexes, performing a select count(PK_COLUMN) is still a considerable amount of IO (needs to reads all data pages).
It's not unusual to see iPhones that, after one year, still have a battery capacity of 95% or above. The reason is a type of fail-safe: Apple builds its batteries with excess capacity, meaning that it doesn't actually use all of its potential operating power when its Battery Health states 100%.
I often write code like this:
if ( list.Count > 0 ) { }
Is this efficient?
Yes. That retrieves the count in the list, which is stored in a field inside the list, and compares it to zero.
Now a question you did not ask:
What about
if ( sequence.Count() > 0 ) { }
? (Notice the parentheses onCount()
.)
We interrogate the sequence at runtime to see if it is a list that has a Count
property that can be computed efficiently. If it does, we call it. If not, we count the entire sequence one item at a time, and then compare that to zero.
Isn't that incredibly inefficient?
Yes.
What would be more efficient?
if (sequence.Any())
Why is that more efficient?
Because it attempts to iterate over one element. If it succeeds, then Any
is true; if it fails then Any
is false. You don't need to count the number of jellybeans in the jar in order to know if there are more than zero. You only need to look to see if there is at least one.
In addition to being considerably more efficient, the code now reads like the intended meaning of the code. If you are intending to ask "are there any items in the list?" then ask "are there any items in the list?" and not "is the number of items in the list greater than zero?"
What about the
Capacity
property of a list?
That tells you how much space has been pre-allocated in the list's internal data structures. It is the amount of items the list can store before it has to allocate more memory.
The Count
property in List<T>
- and all other ICollection<T>
implementations in the BCL - is an O(1) operation, that means it is fast and independent of the number of the number of elements in the list.
There also exists an extension method Count()
that can be called on any IEnumerable<T>
. This method is O(n), which means its runtime depends on the number of elements in the enumerable. However, there is one exception: If the enumerable really is an implementation of ICollection<T>
or ICollection
it uses the Count
property making it again an O(1) operation.
The Capacity
property is normally nothing you need to worry about.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With