0

What is the overall runtime of inserting into a multi set, lets say I am going over a billion elements and inserting into multi-set, which maintains a sorted ordering. What is my worst case runtime?

AyBayBay
  • 1,726
  • 4
  • 18
  • 37
  • 1
    You mean complexity? Do you mean STL multisets? Please be a little bit more specific. One-line questions are usually quite bad. – Uli Köhler Jan 26 '14 at 01:54
  • 1
    http://en.cppreference.com/w/cpp/container/multiset/insert#Complexity – Sebastian Hoffmann Jan 26 '14 at 01:56
  • If you mean actual runtime in seconds, then it depends for one thing on how long it takes to copy/move that type of object and the question cannot really be answered. Run it and see. If you mean complexity, then the number "a billion" is completely irrelevant (although it does for example rule out doing it at all on a 32 bit machine). – Steve Jessop Jan 26 '14 at 02:46

1 Answers1

1

According to http://www.sgi.com/tech/stl/MultipleAssociativeContainer.html the complexity of insert is O(log n) for inserting a single element; for inserting a sequence of length N, it is O(N log n).

If you really want the time, and not the asymptotic complexity, you can time it for a different values - 1000, 10,000 - say, and then compute the constants of proportionality from there. The actual equation will be t = A n log n + C.

But of course the next time you run on different hardware the values of A and C will change.

DRVic
  • 2,481
  • 1
  • 15
  • 22
  • the `+ C` part should be negliable (lets pretend we are physicists :D ) – Sebastian Hoffmann Jan 26 '14 at 10:09
  • "The actual equation will be `t = A n log n + C`" -- not necessarily. It could for example be `A n log n + B n + C` and would still be `O(n log n)`. A term proportional to `n` is fairly likely, I would guess, since `n` is the number of elements created in the multiset, and creating the element (ignoring the part about finding where to put it) is likely to have roughly constant cost. And as Paranaix says, the `C` is likely to be tiny: it's the time taken to insert zero elements into a multiset :-) – Steve Jessop Jan 26 '14 at 18:44
  • It becomes negligible in the limit of high values of N. But for calculating the value of A, you can use much smaller values of N, and then the fit will be better if you don't ignore C. – DRVic Jan 26 '14 at 22:52
  • And yes, there probably will be a term proportional to `n` – DRVic Feb 01 '14 at 18:01