Mohamad Shiralizadeh
Mohamad Shiralizadeh

Reputation: 8765

Difference between list.Count > 0 and list.Count != 0

I have a list of things. Is there any difference between list.Count > 0 and list.Count != 0? Or any performance difference in these codes?

if (list.Count > 0)
    // do some stuff

if (list.Count != 0)
    // do some stuff

note: list.Count Can't be less than ziro..

Upvotes: 1

Views: 10736

Answers (7)

adewale
adewale

Reputation: 1

There can be difference in certain scenarios. For example, if list is null and your check looked like this:

if (list?.Count != 0)

then this expression would evaluate to true since null != 0, but if your check looked like this:

if (list?.Count > 0)

it would evaluate to false.

Upvotes: -1

Mohi
Mohi

Reputation: 1808

I guess in the CPU registers level, the assembly commands for comparing two numbers will check those numbers bit by bit, and will activate some conditional flags to jump to specified lines. for Example:

cmp BL, BH     ; BL and BH are cpu registers
je EQUAL_Label       ; BL = BH
jg GREATER_Label     ; BL > BH
jmp LESS_Label       ; BL < BH

As you can see, je, jg or jmp commands are almost the most atomic commands that probably work on previous cmp(compare) command. In conclusion we can say there is no significant performance difference between those compare command.
Good luck

Upvotes: 1

Arghya C
Arghya C

Reputation: 10078

As explained by everyone, functionally there's no difference between list.Count != 0 and list.Count > 0 as list.Count can not be < 0.

I did a quick test, and it shows both != 0 and > 0 are almost equally fast (and that's super fast). Linq's list.Any() is NOT as fast though!

Here's the test code (comparing 100000 times to magnify the difference)

static List<object> GetBigObjectList()
{
    var list = new List<object>();
    for (int i = 0; i < 10000; i++)
    {
        list.Add(new object());
    }
    return list;
}

static void Main(string[] args)
{
    var myList = GetBigObjectList();
    var s1 = new Stopwatch();
    var s2 = new Stopwatch();
    var s3 = new Stopwatch();

    s1.Start();
    for (int i = 0; i < 100000; i++)
    {
        var isNotEqual = myList.Count != 0;
    }
    s1.Stop();

    s2.Start();
    for (int i = 0; i < 100000; i++)
    {
        var isGreaterThan = myList.Count > 0;
    }
    s2.Stop();

    s3.Start();
    for (int i = 0; i < 100000; i++)
    {
        var isAny = myList.Any();
    }
    s3.Stop();

    Console.WriteLine("Time taken by !=    : " + s1.ElapsedMilliseconds);
    Console.WriteLine("Time taken by >     : " + s2.ElapsedMilliseconds);
    Console.WriteLine("Time taken by Any() : " + s3.ElapsedMilliseconds);            
    Console.ReadLine();
}

And it shows:

Time taken by != : 0
Time taken by > : 0
Time taken by Any() : 10

Upvotes: 2

qxg
qxg

Reputation: 7036

I guess if ICollection.Count is type of uint, you will not have such question. See why ICollection.Count is int at Why does .NET use int instead of uint in certain classes?.

From readability point of view, list.Count != 0 will cause thinking whether Count can be negative. So I prefer list.Count > 0 personaly.

Upvotes: 0

Timothy Stepanski
Timothy Stepanski

Reputation: 1196

There's realistically no difference as the list can never have less than 0 items, but == for integral comparisons is wicked fast, so it's probably faster than >. A cooler looking approach is list.Any().

(This is assuming by list you mean the List type or any built in IEnumerable/Collection)

Upvotes: 7

sujith karivelil
sujith karivelil

Reputation: 29036

In this particular scenario There is no typical difference between these two. but != and >0 are entirely different. >0 Execute only when the count(conditional expression)greater than 0 where as != Execute when the count(conditional expression) greater than 0 as well as for less than 0

if (list.Count > 0)
{
  // Execute only when the count greater than 0
}
if (list.Count != 0)
{
  // Execute only when the count not equal to 0
}

Upvotes: 0

juanchopanza
juanchopanza

Reputation: 227468

Is there any difference between list.Count > 0 and list.Count != 0?

Yes. The first one evaluates whether list.Count is greater than 0. The second one evaluates whether it is not equal to 0. "Greater than" and "not equal" are different things.

Upvotes: 0

Related Questions