Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
@datanerd
Count() will evaluate the condition on all elements in the list. Now suppose that list to be extremely huge, and the condition's evaluation to be relatively complex.
In this particular example it would be way more efficient to write for example:
var element = HugeList.FirstOrDefault(Condition);
if(element != null)
DoSomething(element); -
I'm just gonna say it: Don't use these stupid functional list manipulation methods full stop if you care about performance
-
@12bitfloat
Lets write everything in assembly then...
Is the same piece of code slower using LINQ than using loops?
Yes
Does that mean that whenever you use LINQ you shouldn't be mindful about performance?
Absolutely not. -
@12bitfloat actually if you're good enough in Linq, performance wouldn't be an issue
-
@12bitfloat That’s the most retarded comment I’ve read on devrant in a very long time.
Most of the time these functions implement very good algos. Keep writing your bubble sort manually.
I can agree, that you need to understand HOW they work in order to avoid triple (or even 10x times) iteration of the initial list. But in 95% of cases even “badly” written statement will be faster than half-baked “custom” solution. And there are special cases when Database is involved. -
@NoToJavaScript I didn't talk about sorting. In his example he iterates over a list to find the first element passing the condition. Is it really necessary to slap more layers of complexity on top of a simple solution?
-
@NoToJavaScript Also funny because you would probably be the first one to speak up against the millions of dependencies of a typical JavaScript app...
-
@12bitfloat (btw, I have nothing against TypeScript, I just hate writing JavaScript without strong types, "find references", "rename" and other good IDE stuf)
It depends. I like using “map” and “filter” in JS. And no, I don't like 100000 libs in front end
But let’s just see what happens if I slightly edit the original code. I only added toArray.
var filteredList = hugeList.Where(x => x.Number == 1).ToArray();
Result ? The list was enumerated only once. (note : items are not cloned with ToArray, so memory overhead is very small).
Edit : FirstOrDefault solution is still better
Related Rants
Ffs
var filteredList = HugeList. Where(Condition);
if(filteredList.Count() > 0)
DoSomething(filteredList.First());
Just no. Please.
rant
c#
firstordefault
linq
10th time today
ffs