I’m making a video game where performance is critical.
I’m using the .Distinct() extension method to get unique value from a List.
Is there a faster way to do so? (even if it means having many more lines of code)
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
.Distinctis anO(n)call.You can’t get any faster than that.
However, you should make sure that your
GetHashCode(and, to a lesser extent,Equals) is as fast as possible.Depending on your scenario, you may be able to replace the
List<T>with aHashSet<T>, which will prevent duplicates from being inserted in the first place. (yet hasO(1)insertion)However, Always profile your code before jumping to conclusions about what needs to be faster.