简体   繁体   English

在C ++中复制变量的复杂性是什么

[英]What is the complexity of copying a variable in C++

A friend of mine said that bitmasks are better than bitsets because it takes O(N) time to copy a bitset whereas only O(1) time to copy a bitmask, making it far faster for applications such as in Dynamic Programming. 我的一个朋友说,位掩码比位集更好,因为复制位集需要O(N)时间,而复制位掩码只需要O(1)时间,因此对于动态编程等应用程序来说,它要快得多。 So I was wondering is it the same for all other data types? 所以我想知道所有其他数据类型是否相同?

For eg what would be the complexity of this block of code? 例如,此代码块的复杂性是什么?

vector <int> vec1 = {1,2,3,4,5,6,7,8,9,10};
vector <int> vec2 = vec1;

If it is O(N), why is it so? 如果是O(N),为什么会这样呢? Shouldn't it be O(1) just like doing something like 不应该像做某件事一样是O(1)

int x = 530210;
int y = x;

would be? 将会?

You have vec1 which is a std::vector<T> represented by this truck with a black-container attached to its trailer. 您有vec1 ,这是此卡车代表的std::vector<T> ,其拖车上连接了黑色容器

在此处输入图片说明

(source: bigcommerce.com) (来源:bigcommerce.com)

Now, there are millions of papers in its black-container (by black-container , std::vector<T> 's memory for the elements). 现在,它的黑色容器中有数百万篇论文(由black-container提供std::vector<T>对元素的存储)。 Now, you want to photocopy the papers into another similar vehicle, vec2 . 现在,您想将纸张复印到另一个类似的载体vec2 How best can you do it? 你怎么能做到最好? What is the photo- copy complexity? 影印的复杂度是多少? It is going to be O(Number of papers) 将会是O(论文数量)


Now, assuming you want to move the contents... All std::vector needs to do is to detach its black-container from the truck, and attach it into another truck's... That makes move complexity of the container to be O(1) 现在,假设您要移动内容...所有std::vector需要做的就是从卡车上拆下其黑色容器 ,并将其连接到另一辆卡车上。...这会使容器的移动复杂度变为O (1)

To understand the complexity of an operation, you first have to define your model of computation. 要了解操作的复杂性,首先必须定义您的计算模型。 In particular, you have to define what operations are O(1) and what operations you consider free. 特别是,您必须定义哪些操作是O(1)以及哪些操作是免费的。

For example, if you are looking at programs needing a lot of hard disk access, you would only consider reading a block from disk as one operation and memory access as free. 例如,如果您正在查看需要大量硬盘访问的程序,则只会考虑从磁盘读取一个块作为一项操作,而对内存的访问则是免费的。

In the example of bitmasks and bitssets, you are probably counting memory block copy operations. 在位掩码和位集的示例中,您可能正在计算内存块复制操作。 Then copying a bit mask that fits into one memory block costs only one memory copy. 然后,复制适合一个内存块的位掩码仅花费一份内存副本。 However, copying bitmasks is still O(N) in this model because a large bitmask will span many memory blocks. 但是,在此模型中,复制位掩码仍然为O(N),因为大的位掩码将跨越许多存储块。 More precisely, if b is the number of bits in one block of memory and N the number of bits in the bitmask, then you need ceil(N/b) memory block copy operations to copy the bitmask. 更准确地说,如果b是一个内存块中的位数,而N是位掩码中的位数,那么您需要ceil(N / b)存储块复制操作来复制位掩码。

Copying a vector of length N can be measured in several ways: 复制长度为N的向量的方法有几种:

  • O(1) vector copy operations O(1)向量复制操作
  • O(N) element copy operations O(N)元素复制操作
  • O(NT) units of measure, if the element copy operation is O(T) 如果元素复制操作为O(T) ,则为O(NT)个度量单位

Incidentally, your friend is wrong about bitset , or you misunderstood. 顺便说一句,您的朋友关于bitset是错误的,否则您会误解。 The advantage of bitset is that the data structure is optimized to take advantage of a low level form of parallel execution that can be done by a single thread — ie 64-bit integer operations can be used to work on 64 different bits simultaneously. 的优点bitset是数据结构被优化以利用并行执行的低水平的形式,可以由单个线程来完成的优点-即64位整数运算可以用来在64个不同的位同时工作。

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

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