Skip to content
Prev 6938 / 10988 Next

[Rcpp-devel] How to speed up selection of unique unordered pairs

On Dec 30, 2013, at 1:38 PM, Dirk Eddelbuettel <edd at debian.org> wrote:

            
Since m is unordered, "find all" would need to do O(n) comparisons, whereas the std::set implementation should use a balancing binary search tree that needs only O(log(n)) comparisons, so my intuition is that this wouldn't be faster than your previous implementation.
You know that the objects are pairs, so you should probably use std::pair instead of std::vector.
And if you do an initial pass to identify all unique strings, you should be able to replace strings with an integer index, which would be faster for comparison. (You could also reorder the pairs so that first element is always <= second element, then sort the pairs, and remove duplicates by making another pass to check for identical consecutive rows.)