Difference between list.Count > 0 and list.Count != 0
Asked Answered
L

7

1

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

Laundry answered 10/10, 2015 at 5:15 Comment(7)
If that would make or break your program, you are in good shape.Springtime
https://mcmap.net/q/265077/-is-the-inequality-operator-faster-than-the-equality-operatorGhyll
Why do you care about it, regarding performance or correctness?Equation
@qxg, I am just curious witch one is better and more standard in programmers world..Laundry
No difference. I would say not even a single cycle difference. en.wikibooks.org/wiki/Microprocessor_Design/ALU_Flags also kind of similar questions #18596800 programmers.stackexchange.com/questions/271881/…Ghyll
This is the kind of thing that could depend on the language, the type and implementation of list, and the type of list.Count.Cocteau
There is no functional difference, when you trust that list.Count never returns a value less than zero.Grizelda
S
7

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)

Settler answered 10/10, 2015 at 5:18 Comment(1)
I though in the same lines (upvoted), but in reality list.Any() seems to be slower! Check my answer below :)Belgrade
B
2

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

Belgrade answered 10/10, 2015 at 6:12 Comment(1)
Years later, but I hope that you've heard of this library by now to use for benchmarks like this: github.com/dotnet/BenchmarkDotNetHieronymus
B
1

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

Babysitter answered 27/7, 2016 at 9:32 Comment(0)
C
0

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.

Cocteau answered 10/10, 2015 at 5:18 Comment(4)
Can we assume that they mean a List which wouldn't have less than 0?Settler
@TimothyStepanski You should ask OP. I am making no such assumption.Cocteau
count of list is never less than zero so here they are same.Ghyll
@Cocteau You should make assumptions. You have all the info to make them in this case.Latrinalatrine
P
0

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
}
Pencel answered 10/10, 2015 at 5:20 Comment(2)
How can you know that? If you have access to OP's code, could you tell us what language it is written in, and what list is?Cocteau
Ya well. List.Count will not be negative. What i am trying to say in my second statement is the difference between >0 and !=Pencel
E
0

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.

Equation answered 10/10, 2015 at 5:49 Comment(0)
L
-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.

Lyford answered 10/9, 2024 at 12:12 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.