Note that there are some explanatory texts on larger screens.

plurals
  1. POEfficient set intersection of a collection of sets in C++
    primarykey
    data
    text
    <p>I have a collection of <code>std::set</code>. I want to find the intersection of all the sets in this collection, in the fastest manner. The number of sets in the collection is typically very small (~5-10), and the number of elements in each set is is usually less than 1000, but can occasionally go upto around 10000. But I need to do these intersections tens of thousands of time, as fast as possible. I tried to benchmark a few methods as follows:</p> <ol> <li>In-place intersection in a <code>std::set</code> object which initially copies the first set. Then for subsequent sets, it iterates over all element of itself and the ith set of the collection, and removes items from itself as needed.</li> <li>Using <code>std::set_intersection</code> into a temporary <code>std::set</code>, swap contents to a current set, then again find intersection of the current set with the next set and insert into the temp set, and so on.</li> <li>Manually iterate over all the elements of all sets like in 1), but using a <code>vector</code> as the destination container instead of <code>std::set</code>.</li> <li>Same as in 4, but using a <code>std::list</code> instead of a <code>vector</code>, suspecting a <code>list</code> will provide faster deletions from the middle.</li> <li>Using hash sets (<code>std::unordered_set</code>) and checking for all items in all sets.</li> </ol> <p>As it turned out, using a <code>vector</code> is marginally faster when the number of elements in each set is small, and <code>list</code> is marginally faster for larger sets. In-place using <code>set</code> is a substantially slower than both, followed by <code>set_intersection</code> and hash sets. Is there a faster algorithm/datastructure/tricks to achieve this? I can post code snippets if required. Thanks! </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload