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

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.

Related

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

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?

When an object is equal to another object in test methods? [duplicate]

This question already has answers here:
C# - Asserting two objects are equal in unit tests
(8 answers)
Comparing Two objects using Assert.AreEqual()
(5 answers)
Closed 4 years ago.
I want to know, when an object is equal or not equal to another object with the method Assert.AreEqual.
I have to objects, which are tested false.
But when I test every single property with AreEqual the tests are successfull.
That would mean equality does not only depend on properties. Could someone explain that?
(It's a theoretical question, I cannot provide any data)

Is there a way to get all the possible return values of a method in C#? [duplicate]

This question already has an answer here:
Is using static bytecode analysis to determine all the possible paths through a given method a variant of trying to solve the Halting Problem?
(1 answer)
Closed 4 years ago.
Suppose there's a method which returns enum. But it returns only a subset of all the values of the enumeration. Can I find out programmatically which values are obtainable?
Example. I have an enum, which describes color with 100 values. Method GetCurrentTrafficLightsState can return only 3 colors of 100. I want to pass the method GetCurrentTrafficLightsState into some other method and get 3 colors as response.
No there isn't. You cannot even determine if it will return at all, see https://en.wikipedia.org/wiki/Halting_problem

Get all combinations of an array ( if i remove them 1 by 1 ) [duplicate]

This question already has answers here:
Algorithm to return all combinations of k elements from n
(77 answers)
What is the best way to find all combinations of items in an array?
(11 answers)
Closed 9 years ago.
Given:
{1,2,3}
Expected result:
{1,2,3},
{1,2},{1,3},{2,3},
{1},{2},{3}
So I want basically ALL possible combinations in a list ( but including all possible combinations of - when every element is removed ).
I hope you get what I mean ;)
Question: Which algorithm achieves this?
You want the power set algorithm.
There are some examples on Rosetta Code.

Whats the point of Tuple(Of T) [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What's the purpose of the Tuple(T1)/Singleton in .net?
Trying to mimic a Tuple as implemented in .Net 4 (For .Net 3) I just realized there is a Tuple(Of T)? This was quite a surprize!
Why would anyone do this
Tuple<string> result = new Tuple<string>("Data");
Instead of this
return "Data";
Isn't the whole point of a tuple that its a container for "loosely related data that isnt cohesive enough to make another class"? Am I missing something?
There are a finite number of tuple-arities in the library, so to define an 8-tuple, you use the kind with 7-elements whose 'rest' argument is a one-tuple. See
http://msdn.microsoft.com/en-us/library/dd383325.aspx
This is a carry over from set theory that might not have much use for a software developer.
Tuples are simply ordered lists of elements. An N-tuple has n elements, and n can be one, which is called a singleton. You probably won't have much use for a 1-tuple in code, but I'm guessing the C# team put it in there for completeness.
http://en.wikipedia.org/wiki/Tuple#Etymology

Categories