2

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

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
Mohamad Shiralizadeh
  • 8,329
  • 6
  • 58
  • 93
  • 3
    If that would make or break your program, you are in good shape. – R Sahu Oct 10 '15 at 05:18
  • http://stackoverflow.com/a/1030001/4767498 – M.kazem Akhgary Oct 10 '15 at 05:23
  • Why do you care about it, regarding performance or correctness? – qxg Oct 10 '15 at 05:38
  • @qxg, I am just curious witch one is better and more standard in programmers world.. – Mohamad Shiralizadeh Oct 10 '15 at 05:41
  • 1
    No difference. I would say not even a single cycle difference. https://en.wikibooks.org/wiki/Microprocessor_Design/ALU_Flags also kind of similar questions http://stackoverflow.com/questions/18596300/performance-greater-smaller-than-vs-not-equal-to http://programmers.stackexchange.com/questions/271881/is-a-1-10-comparison-less-expensive-than-1-1000000 – M.kazem Akhgary Oct 10 '15 at 05:45
  • This is the kind of thing that could depend on the language, the type and implementation of `list`, and the type of `list.Count`. – juanchopanza Oct 10 '15 at 05:47
  • There is no functional difference, when you trust that list.Count never returns a value less than zero. – oɔɯǝɹ Oct 10 '15 at 12:17

6 Answers6

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)

Arghya C
  • 9,805
  • 2
  • 47
  • 66
Timothy Stepanski
  • 1,186
  • 7
  • 21
  • I though in the same lines (upvoted), but in reality `list.Any()` seems to be slower! Check my answer below :) – Arghya C Oct 10 '15 at 06:23
3

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

Arghya C
  • 9,805
  • 2
  • 47
  • 66
  • 1
    Years later, but I hope that you've heard of this library by now to use for benchmarks like this: https://github.com/dotnet/BenchmarkDotNet – Eddie Hartman Mar 31 '22 at 18:01
2

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.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
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

Mohi
  • 1,776
  • 1
  • 26
  • 39
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
}
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
  • 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? – juanchopanza Oct 10 '15 at 05:27
  • Ya well. List.Count will not be negative. What i am trying to say in my second statement is the difference between `>0` and `!=` – sujith karivelil Oct 10 '15 at 05:34
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.

Community
  • 1
  • 1
qxg
  • 6,955
  • 1
  • 28
  • 36