'Always Use Any over Count in LINQ' | Code Cop #008

preview_player
Показать описание


Hello everybody, I'm Nick, and in this video, I'll show you how problematic it can be to use benchmarks to prove a point when the benchmark technique is not very good.

Don't forget to comment, like and subscribe :)

Social Media:

#csharp #dotnet
Рекомендации по теме
Комментарии
Автор

Better advice: "Understand the runtime complexity of operations on collections". There are many nuances depending on the collection type, the data distribution, etc. It's true that Any will likely never be slower than Count, but as in your example, maybe neither one is very good because the underlying collection (a database in this case) isn't optimized to answer either operation efficiently.

carldaniel
Автор

Hi Nick,
This is exactly the explanation I was looking for, since I came across the same post today. And I also had the 2 points that you've covered in this video. I did try to convey my disagreement by replying my explanation but guess it wasn't detailed enough to convey my understanding to author of that post, to that he replied back with his own version. Now I think I understand this concept evidently better than before. Thanks for helping out with the concepts here Nick 😊
Always Grateful 🙏

shanksemp
Автор

I've found Count() is actually faster than Any(), when I include parsing of a 200GB CSV file as part of the Any predicate. I think I'll post up some general advice on LinkedIn.

DavidWengier
Автор

I'm curious what SQL the EF version translates to. The expression-to-SQL converter should be able to detect and optimize the Count(...) > 0 case and convert it to the equivalent of Any(...) or some other optimized version.

dnwheeler
Автор

I see the title card with you wearing the policeman’s cap and all I can think of is the Village People.

joelinherts
Автор

Even though the benchmark methodology was wrong, the suggestion was correct; you should use Any over Count, from both a performance and readability perspective.

RandallEike
Автор

A connoisseur of C# will appreciate the beauty of the Hungarian notation 'bln' prefix and how wrapping the return value in parenthesis somehow improves the code... 😜

MartinLiversage
Автор

@nickchapsas

I thought this was all common sense. At least that's how I see LINQ should be used.

There's one slight definition of yours that I strongly disagree with.
Using Any, Count or anything defined in System.Linq namespace on EF or EF Core is LINQ.


You say it's not LINQ, but it actually is.
DbSet<T> implements IQueryable<T> which is a type of LINQ data source.
It uses different mechanism (Expression Trees) than IEnumerable. Expression Trees are powerful and remind me of abstract syntax tree.
Performance depends on how Expression Trees are interpreted by IQueryProvider for given data source (IQueryable).

dusanknezevic
Автор

A Count in SQL isn't even that bad. How it performs is going to depend on your data, how your indexes are set up, and what execution plan you end up with. You're not always going to end up with an index scan.

exmello
Автор

This is the exact reason why I never trust anyone's benchmarks, some are just wrong, and some others while useful do not really represent what real world applications have.

PticostaricaGS
Автор

I saw the post and I was waiting for this video.

codewkarim
Автор

Love your videos. Could you make a video on handling big data sets 🤔

Arshaad
Автор

But you benchmarks proved that original advice still holds good. Always prefer using Any over count for existence check. May not be significant enough improvement. But still valid advice. Atleast valid enough to not make this video...poor case picked up by the code cop...LOL

askids
Автор

Reaching on this. Running it with the provider is important. It's can be different with the provider.

mrgrovesempire
Автор

I’m a fan on Nick’s videos, but recently it appeared to be safe fast forward like 2/3 of a video to get exactly on a point “Use deterministic test data” done

ADOConnection
Автор

My supervisor claimed lock is faster than interlocked, referencing an 11 year old article that had bad benchmarks. I proved him wrong with proper benchmarks.

protox
Автор

I do see so many projects that has LINQ Count() on arrays and lists.
In that case using native length or count is so much faster

DevDunkStudio
Автор

Micro benchmarks are overrated. Should be more concerned with UI response times, and azure compute costs. Would like to see more big picture type of benchmarks.

haxi
Автор

Video should be called "How to benchmark" tbh. And yes you should use Any over Count if that's all you need. This advice is perfectly fine, how it was presented could have been better.

junkman
Автор

Lucky I don’t ever look for code on LinkedIn.
When is the next live stream Nick?

tanglesites