我决定对简单类型(例如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)