On Thu, 2019-01-17, Paul wrote:
> Suppose two vectors of ints are the same size -- say one million entries each.
> Comparisons need to be made between elements in the vectors -- for example
> it needs to be queried where A[780] < B[781].
Did you mean A[780] < B[780]?
> The same information that can be stored in two vectors can also be stored
> as a single vector of std::pair<int, int>.
> Does a vector of pair<int, int> take up less space than two vectors?
> Is there any difference in performance between an enquiry to determine
> whether A[780].first < A[781].second compared to the above inquiry
> with separate vectors.
>
> Naively, I would think a std::pair approach is preferred. My guess is that
> it takes less space. Also, is there an architectural point to do with caches
> (my knowledge about this is hopelessly vague) that says that comparing
> A[k] with B[m] is slow because these two vectors reside in unrelated
> areas of memory whereas comparing A[k].first with A[m].second is faster
> because the A entries are in the same area of memory?
>
> Any thoughts?
A vector of pairs takes up roughly as much space, and lookup in that
one is nicer to the cache than two lookups in two vectors[0].
What bothers me is: is this really the main use case for these
vectors? Are they created once, never updated, and only used to
answer the question a[N] < b[N]? If not, you're going to make some
other code uglier and slower. If yes, then a vector<bool> would be
nicer to the cache.
/Jorgen
[0] Although I cannot say how much of a difference that would make.
It depends on the overall access pattern.
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/
snipabacken.se> O o .