小编dFi*_*nov的帖子

对于简单类型C++,使用静态tmp变量重新实现std :: swap()

我决定对简单类型(例如int,或者struct,或者class在其字段中仅使用简单类型的交换函数)的实现进行基准测试,并在其中使用statictmp变量来防止每次交换调用中的内存分配.所以我写了这个简单的测试程序:

#include <iostream>
#include <chrono>
#include <utility>
#include <vector>


template<typename T>
void mySwap(T& a, T& b)     //Like std::swap - just for tests
{
    T tmp = std::move(a);
    a = std::move(b);
    b = std::move(tmp);
}

template<typename T>
void mySwapStatic(T& a, T& b)   //Here with static tmp
{
    static T tmp;
    tmp = std::move(a);
    a = std::move(b);
    b = std::move(tmp);
}

class Test1 {       //Simple class with some simple types
    int foo;
    float bar; …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm c++11

4
推荐指数
1
解决办法
339
查看次数

标签 统计

algorithm ×1

c++ ×1

c++11 ×1