我刚刚编写了一个测试程序,以找到分配和释放许多管理对象的最快方法shared_ptr
.
我试图shared_ptr
用new
,shared_ptr
用pool
,make_shared
,allocate_shared
.是什么让我感到惊讶的allocate_shared
是慢shared_ptr
用pool
.
我vs2017+win10
用发布版本测试代码.发布版本设置为默认值(/ O2).我也测试它gcc4.8.5+centos6.2
与g++ -std=c++11 -O3
.
代码是:
#include <memory>
#include <iostream>
#include <vector>
#include <assert.h>
#include <chrono>
#include <mutex>
using namespace std;
struct noncopyable {
protected:
noncopyable() = default;
~noncopyable() = default;
private:
noncopyable(const noncopyable&) = delete;
noncopyable& operator=(const noncopyable&) = delete;
noncopyable(noncopyable&&) = delete;
noncopyable& operator=(noncopyable&&) = delete;
};
class BlockPool : noncopyable …
Run Code Online (Sandbox Code Playgroud) 我对一件事感到非常困惑...如果我将构造函数添加到 struct A 中,那么 for 循环中的计算会变得慢很多倍。为什么?我不知道。
在我的计算机上,输出中的代码片段的时间为:
有构造函数:1351
没有构造函数:220
这是一个代码:
#include <iostream>
#include <chrono>
#include <cmath>
using namespace std;
using namespace std::chrono;
const int SIZE = 1024 * 1024 * 32;
using type = int;
struct A {
type a1[SIZE];
type a2[SIZE];
type a3[SIZE];
type a4[SIZE];
type a5[SIZE];
type a6[SIZE];
A() {} // comment this line and iteration will be twice faster
};
int main() {
A* a = new A();
int r;
high_resolution_clock::time_point t1 = high_resolution_clock::now();
for (int i …
Run Code Online (Sandbox Code Playgroud) c++ optimization default-constructor compiler-optimization visual-studio-2013
我无法理解 gcc 4.8.1 或 Visual Studio 2015 在默认初始化与值初始化方面的行为。
我试图自己理解这些之间的差异并可能遇到编译器错误并没有帮助?
我的问题是:有人可以解释这种行为吗?最好告诉我应该发生什么。
我有两个班级:
class Foo{
int _bar;
public:
void printBar(){ cout << _bar << endl; }
};
class bar{
int ent;
public:
int getEnt(){return ent;}
};
Run Code Online (Sandbox Code Playgroud)
我正在使用以下代码进行测试:
int main()
{
Foo foo;
foo.printBar();
Foo().printBar();
bar b;
cout << b.getEnt() << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在 gcc 和 Visual Studio 上,我得到:
134514795
0
0
现在,如果我将测试代码更改为:
int main()
{
Foo foo;
foo.printBar();
bar b;
cout << b.getEnt() << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
gcc …