如果没有vector呢,只有int和double, 哪个开销更大
时间: 2024-03-28 20:41:00 浏览: 12
在计算机中,int 和 double 的大小是固定的,分别是4字节和8字节。因此,如果仅考虑单个变量的大小,int 的开销更小。但是,如果考虑存储一组变量时的开销,需要考虑内存对齐等因素。此外,如果涉及到计算,double 的精度比int更高,因此在某些情况下使用double可能会更加有效。总的来说,需要根据具体的场景来选择使用哪种类型。
相关问题
如果mystruct中的 data是 vector而不是指针呢, 哪个开销更大
如果 `MyStruct` 中的 `data` 是 `vector` 类型而不是指针类型,使用移动语义的开销仍然比使用拷贝构造函数的开销要小。这是因为 `vector` 类型已经实现了移动构造函数和移动赋值运算符,可以直接将原对象中的内存空间移动到新对象中,而不需要进行分配和复制操作。
下面是一个使用 `vector` 类型的例子,可以看到使用移动语义的开销仍然比使用拷贝构造函数的开销要小:
```cpp
#include <iostream>
#include <vector>
#include <chrono>
using namespace std;
using namespace std::chrono;
struct MyStruct {
int id;
double score;
vector<int> data;
MyStruct() : id(0), score(0.0), data{} {}
MyStruct(const MyStruct& other) : id(other.id), score(other.score), data(other.data) {}
MyStruct& operator=(const MyStruct& other) {
if (this != &other) {
id = other.id;
score = other.score;
data = other.data;
}
return *this;
}
MyStruct(MyStruct&& other) : id(other.id), score(other.score), data(move(other.data)) {}
MyStruct& operator=(MyStruct&& other) {
if (this != &other) {
id = other.id;
score = other.score;
data = move(other.data);
}
return *this;
}
};
int main() {
MyStruct a{1, 90.5, {1,2,3,4,5}};
auto start1 = high_resolution_clock::now();
MyStruct b = move(a); // 使用移动构造函数
auto end1 = high_resolution_clock::now();
auto duration1 = duration_cast<microseconds>(end1 - start1);
cout << "使用移动构造函数的时间:" << duration1.count() << " 微秒" << endl;
MyStruct c{2, 85.5, {6,7,8,9,10}};
auto start2 = high_resolution_clock::now();
MyStruct d = c; // 使用拷贝构造函数
auto end2 = high_resolution_clock::now();
auto duration2 = duration_cast<microseconds>(end2 - start2);
cout << "使用拷贝构造函数的时间:" << duration2.count() << " 微秒" << endl;
return 0;
}
```
在上面的例子中,我们使用 `vector` 类型替换了 `MyStruct` 中的指针成员变量 `data`,并且重载了拷贝构造函数、赋值运算符函数和移动构造函数、移动赋值运算符函数。在进行移动操作时,我们直接使用 `move` 函数将原对象中的 `vector` 移动到新对象中,而不需要重新分配和复制内存。可以看到,使用移动构造函数的开销仍然比使用拷贝构造函数的开销要小。
可以举例说明,使用move的开销更大吗
下面是一个使用 move 语义的例子,可以看出使用 move 语义的开销更大:
```cpp
#include <iostream>
#include <vector>
#include <chrono>
using namespace std;
using namespace std::chrono;
struct MyStruct {
int id;
double score;
vector<int> data;
};
int main() {
MyStruct a{1, 90.5, {1,2,3,4,5}};
auto start1 = high_resolution_clock::now();
MyStruct b = move(a); // 使用移动构造函数
auto end1 = high_resolution_clock::now();
auto duration1 = duration_cast<microseconds>(end1 - start1);
cout << "使用移动构造函数的时间:" << duration1.count() << " 微秒" << endl;
MyStruct c{2, 85.5, {6,7,8,9,10}};
auto start2 = high_resolution_clock::now();
MyStruct d = c; // 使用拷贝构造函数
auto end2 = high_resolution_clock::now();
auto duration2 = duration_cast<microseconds>(end2 - start2);
cout << "使用拷贝构造函数的时间:" << duration2.count() << " 微秒" << endl;
return 0;
}
```
在上面的例子中,我们定义了一个包含一个 vector 类型成员变量的 struct 类型 `MyStruct`。我们创建了两个 `MyStruct` 类型的对象 `a` 和 `c`,并对它们进行移动构造和拷贝构造操作。在移动构造 `a` 到 `b` 的过程中,使用了 move 语义,而在拷贝构造 `c` 到 `d` 的过程中,则使用了默认的拷贝构造函数。
我们在程序中使用了 C++ 标准库中的 `chrono` 库来计算两个操作的时间。我们可以看到,在这个例子中,使用移动构造函数的时间要比使用拷贝构造函数的时间长得多,这说明在只包含简单类型的 struct 对象中,使用 move 语义反而会增加开销。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)