Is “==” in sorted array not faster than unsorted array? [duplicate]

One thing that immediately comes to mind is CPU’s branch prediction algorithm. In case of > comparison, in sorted array the branching behavior is very consistent: first, the if condition is consistently false, then it is consistently true. This aligns very well with even the simplest branch prediction. In unsorted array the result of > … Read more

When to use a SortedList over a SortedDictionary?

I’m not sure how accurate the MSDN documentation is on SortedList and SortedDictionary. It seems to be saying both are implemented using a binary search tree. But if the SortedList uses a binary search tree, why would it be much slower on additions than SortedDictionary? Anyway, here are some performance test results. Each test operates … Read more

What is the SortedList working with RecyclerView.Adapter?

SortedList handles the communication to the Recycler adapter via Callback. One difference between SortedList and List is seen in the addAll helper method in the sample below. public void addAll(List<Page> items) { mPages.beginBatchedUpdates(); for (Page item : items) { mPages.add(item); } mPages.endBatchedUpdates(); } Keeps last added item Say I have 10 cached items to load … Read more

Get random sample from list while maintaining ordering of items?

Following code will generate a random sample of size 4: import random sample_size = 4 sorted_sample = [ mylist[i] for i in sorted(random.sample(range(len(mylist)), sample_size)) ] (note: with Python 2, better use xrange instead of range) Explanation random.sample(range(len(mylist)), sample_size) generates a random sample of the indices of the original list. These indices then get sorted to … Read more

C# Sortable collection which allows duplicate keys

Use your own IComparer! Like already stated in some other answers, you should use your own comparer class. For this sake I use a generic IComparer class, that works with anything that implements IComparable: /// <summary> /// Comparer for comparing two keys, handling equality as beeing greater /// Use this Comparer e.g. with SortedLists or … Read more

SortedList, SortedDictionary and Dictionary

When iterating over the elements in either of the two, the elements will be sorted. Not so with Dictionary<T,V>. MSDN addresses the difference between SortedList<T,V> and SortedDictionary<T,V>: The SortedDictionary(TKey, TValue) generic class is a binary search tree with O(log n) retrieval, where n is the number of elements in the dictionary. In this respect, it … Read more

What’s the difference between SortedList and SortedDictionary?

Yes – their performance characteristics differ significantly. It would probably be better to call them SortedList and SortedTree as that reflects the implementation more closely. Look at the MSDN docs for each of them (SortedList, SortedDictionary) for details of the performance for different operations in different situtations. Here’s a nice summary (from the SortedDictionary docs): … Read more