Fast way to search through an array of elements [duplicate] - c#

This question already has answers here:
FindAll vs Where extension-method
(5 answers)
Slow LINQ query for .ToArray()
(3 answers)
C# FindAll VS Where Speed
(5 answers)
Closed 3 years ago.
I have an array of structs and I need to locate all the elements that share a certain condition.
Currently I use:
Array.FindAll(someArray, x => x.eg == "*Perfomance Test*")
But after some googling I noticed that there is a much faster way by using the LINQ Where-method.
someArray.Where(x => x.eg == "*Perfomance Test*")
I did some testing and the results are pretty impressive:
FindAll: 00:00:03.06
Where: 00:00:00.20
The problem is that Where returns IEnumerable. And I call this method that returns all these certain elements from the array within the for loop. Which is the type of loop I need to use. I used the .ToArray() method, but that made it much worse so that FindAll() is the faster approach.
Because of that it seems to me that if I need to get an array of elements, Where is much slower option than FindAll. But maybe I'm missing something.
Are there any better, faster options?

Related

Equivalence of Observable.Do for Enumerable [duplicate]

This question already has answers here:
Why is there no ForEach extension method on IEnumerable?
(21 answers)
Closed 2 years ago.
Theres a Do method in System.Reactive to a execute a mutator for each item in the sequence.
Is there any equivalent method for IEnumerable either in standard library or third parties like morelinq?
LINQ query operators are generally meant to be free from side-effects. You could just use a foreach loop to "do" your thing.
Or do it in a Select or Where method if you don't care about side effects:
enumerable.Select(x => { /*do something here */ return x; })
For a List<T>, there is a ForEach extension method that can be used to execute an Action<T> for each element.

ICollection as argument: Provide List or Array [duplicate]

This question already has answers here:
Array versus List<T>: When to use which?
(16 answers)
Closed 3 years ago.
If some method requires an ICollection<T> as an argument, but I have only an IEnumerable<T> available: Is it better to convert this IEnumerable<T> to a IList<T> or is it better to convert it to an array T[] or shall I convert it to something else or is there no difference at all?
The IEnumerable<T> is only required for this method call, so no further read/write/extend is required.
If you're not going to be accessing the elements, you should use ToList() as explained why here.
If you'll be iterating through the elements, then it'd be better performance-wise to do it via an index (meaning casting them to an array), so that you can use a for loop, instead of a foreach loop.
for is faster than your typical foreach due to it using an index to access each element.

How to iterate over all elements in array(or list) with LINQ? [duplicate]

This question already has answers here:
Linq style "For Each" [duplicate]
(6 answers)
Closed 3 years ago.
I am new to LINQ and i want to switch onto it from traditional loops
I have an array of GameObjects and I just want to iterate over all of them and set one of their component enabled, and I wanna know if there is 1 line elegant way with LINQ to replace foreach or for loop's version
gameObjects.ToList().ForEach(x => {do Stuff with list});
Some reading on forEach in C# however. Could be interesting: https://blogs.msdn.microsoft.com/ericwhite/2009/04/08/why-i-dont-use-the-foreach-extension-method/

C# - Random.Next(Length-1) vs OrderBy(x=>Guid.NewGuid()).First() [duplicate]

This question already has answers here:
Is using Random and OrderBy a good shuffle algorithm? [closed]
(13 answers)
Are GUIDs timely ordered ? If ORDER BY used with a GUID variable type, will records created lately come late?
(3 answers)
Closed 5 years ago.
Which one is faster?
Im doing tests on a ConcurrentDictionary and want to randomize it and just return 1 result. Essentially pick a random result from it.
Which method is faster/more efficient?
Efficiency as in, as little cpu/memory while having low possible errors like conflicts and such.
Method A.
Random rand = new Random();
var result = concDict.ElementAt(rand.Next(concDict.Count() - 1));
Method B.
var result = concDict.OrderBy(x=>Guid.NewGuid()).First();
In my "vague" testing I dont see much difference apart from Method B being more efficient. Method A can succum to the concDict's Count being out of sync with the concDict.ElementAt causing ArgumentOutOfRangeException: 'maxValue' must be greater than zero. while Method B literally cant cause that.

SortedList<K,V> vs SortedDictionary<K,V> vs Dictionary<K,V> [duplicate]

This question already has answers here:
SortedList<>, SortedDictionary<> and Dictionary<>
(6 answers)
Closed 9 years ago.
I have a large collection of small objects, each has a unique string ident. I need to decide which class to use.
MSDN says about the first two
The two classes have similar object
models, and both have O(log n)
retrieval. Where the two classes
differ is in memory use and speed of
insertion and removal
Since I rarely insert, mostly just retrieve it seems both are good for me. What about the plain old Dictionary?
Plain-old dictionary is the best option if you're not interested in sorting (since it's O(1) retrieval). If you're not going to modify the list much you should use SortedList since it uses less memory.

Categories