相关疑难解决方法(0)

为什么allocate_shared和make_shared这么慢

我刚刚编写了一个测试程序,以找到分配和释放许多管理对象的最快方法shared_ptr.

我试图shared_ptrnew,shared_ptrpool,make_shared,allocate_shared.是什么让我感到惊讶的allocate_shared是慢shared_ptrpool.

vs2017+win10用发布版本测试代码.发布版本设置为默认值(/ O2).我也测试它gcc4.8.5+centos6.2g++ -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)

c++ performance shared-ptr c++11

9
推荐指数
1
解决办法
713
查看次数

使用空构造函数会使数组未初始化,从而导致计算速度变慢

我对一件事感到非常困惑...如果我将构造函数添加到 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

6
推荐指数
1
解决办法
273
查看次数

默认初始化与零初始化

我无法理解 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 …

c++ default initialization zero

3
推荐指数
2
解决办法
1975
查看次数