简体   繁体   English

对于什么参数是std :: vector <void*> ::保留O(1)时间操作?

[英]For what argument is std::vector<void*>::reserve an O(1) time operation?

I am interested in at least an approximate answer to: 我对至少一个大概的答案感兴趣:

int n;
...
std::vector<void*> vectorOfPointers;
vectorOfPointers.reserve(n);

For up to which number n does the above mentioned program run in O(1) ? 上述程序在O(1)中最多可以运行n个

Let us suppose that for a laptop running 32 bit Ubuntu with 3 gigabytes of memory, not running any other programs. 让我们假设对于运行32位Ubuntu和3 GB内存的笔记本电脑,不运行任何其他程序。 Is it tens, thousands, millions? 是几万,几百万还是几百万? What information does one need to know to be able to asses such a question? 一个人需要知道哪些信息才能提出这样的问题? Is there a way to find out without running any experiments? 有没有一种方法无需进行任何实验就能找出答案?

I have never studied anything about operating systems but in my imagination, the operating system allocates memory in just 2 steps. 我从未研究过有关操作系统的任何内容,但在我的想象中,操作系统仅通过两步就可以分配内存。 It determines the pointer to start the chunk and the pointer of the end of the chunk. 它确定开始块的指针和块结束的指针。 The situation can be more complicated because the system may have to tidy up in order to get a big enough chunk of memory. 这种情况可能会更加复杂,因为系统可能必须整理以获取足够大的内存块。 But if we suppose that the system runs just this one program, how can we estimate the time needed? 但是,如果我们假设系统仅运行一个程序,那么如何估算所需的时间呢? Are there some other aspects to be considered, besides the fragmentation of memory? 除了内存碎片,还有其他方面需要考虑吗?

EDIT: 编辑:

sorry, I didn't make my question clear. 抱歉,我没有明确说明我的问题。 It is important to consider, that the vector is empty before calling reserve, so no copying of data is required. 重要的是要考虑到向量在调用保留之前为空,因此不需要复制数据。

You can't rely on a O(1) complexity when using reserve() . 使用reserve()时,不能依赖O(1)的复杂性。

Complexity 复杂

linear in the size of the container 容器尺寸成线性

(cf cppreference ) (cf cppreference

Basically, the allocation of the new memory is in constant time, but you will also need to copy the old elements from the previous memory into the new one (hence the linear complexity). 基本上,新内存的分配是固定的,但是您还需要将旧元素从以前的内存复制到新的内存中(因此线性复杂度很高)。

So on an empty vector, reserve will probably have a constant time, but I'm not sure that the standard explicitely states it. 因此,在空向量上,储备金可能具有恒定的时间,但是我不确定标准是否明确声明了它。 So it probably depends on the underlying implementation (even if I don't see any reason to not do it). 因此,它可能取决于基础实现(即使我看不到任何理由不这样做)。

From the C++ point of view, the code takes O(1) time (it's linear in the current size of the container, which in your case is always zero). 从C ++的角度来看,代码需要O(1)时间(在容器的当前大小中是线性的,在您的情况下,它始终为零)。

Now, it seems that your question is really about the complexity of allocating m bytes of memory. 现在,看来您的问题确实与分配m字节内存的复杂性有关。 That, I am afraid, is unspecified. 恐怕这是不确定的。

For further discussion, see Time complexity of memory allocation 有关更多讨论,请参见内存分配的时间复杂度

To add to what's already been said in the other question, there are several layers of complexity: 除了在另一个问题中已经说过的内容外,还涉及几层复杂性:

  • Firstly, malloc() needs to maintain its internal data structures. 首先, malloc()需要维护其内部数据结构。 The complexity of doing that is not specified, but one would hope that malloc(m) would not take Θ(m) time. 这样做的复杂性没有指定,但是人们希望malloc(m)不会花费Θ(m)时间。 However, the complexity could well depend on other factors, such as memory fragmentation. 但是,复杂度很可能取决于其他因素,例如内存碎片。
  • Secondly, malloc() may need to request additional memory from the OS. 其次, malloc()可能需要从OS请求额外的内存。 Here, it is not unreasonable to expect the OS to do something with every memory page it allocates (eg wipe it so you don't see someone else's confidential data). 在这里,期望操作系统对它分配的每个内存页都做些事情是合理的(例如擦除它,这样您就不会看到别人的机密数据)。 If this were to happen, the operation would indeed be Θ(m) . 如果发生这种情况,则运算的确为Θ(m)

我能想到的唯一O(1)std::vector::reserve() ,当n <= capacity() ,意味着reserve()不执行任何操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM