Bench-marking List vs Dictionary vs HashSet vs SortedSet

As a developer, most of the times, we tend to use datatypes which are more familiar to us without knowing the pros or cons of using those. When we do unit testing with very less records, then it works smothly without any hickups. But when the code goes to production, we face a lot of changes related to performance or memory as we test with huge amount of records. Prevantion is always better than cure!!
Bench-Marking Criteria
I am going to test the Insert / Search / Remove opperation on the following datatypes to check the performance. I am testing with List, Dictionary, HashSet, SortedSet here. Here, I have used to Stopwatch to capture the time. I have executed the same test several times just to check if the captured results are consistant or not. I have gained confidence on the results as I have seen the tends alike everytime.
Test for adding/Inserting
I have created object of List<String>, Dictionary<int, string>, HashSet<string> and SortedSet<string> before executing the test for add. I am adding 100000 records into each of the items and calculating time of execution separatly.
Below is the code for the same.
Here is the result after addition.
It can be inferred from the test that Adding to Dictionary takes least time followed by List, then HashSet, and finally SortedSet.
Test for Searching
After adding 100000 items to each of the types, I am trying to fetch 1 item from them. I am capturing the time taken to seach the results.
Here is the code for the same.
and results are.
Fetching results from HashSet is fastest followed by SortedSet then Dictionary and finally List
Test for Remove
As part of this test, I am removing only 1 item for the types and capturing respective time taken to perform the task.
And code for that is -
The results are.
Removing from Hashset took the least amount of time followed by SortedSet then by Dictionary then List
For each line of code we write is going to impact the system, so by taking a small step,we can improve the perfomance of the application significantly. So before writing code, we should bench-mark our available options and then go ahead with code after verifying the results. It's not always advisable to use the same thing for all the type of opertaions though. Happy coding !!