#include <stdlib.h>
#include <iostream>
#include <windows.h>
#define N 10000
#define ITER 100000
struct foo {
__int64 start, end, freq;
HANDLE hprocess;
DWORD oldclass;
foo() : hprocess(GetCurrentProcess()), oldclass(GetPriorityClass(hprocess)) {
Sleep(10);
// SetPriorityClass(hprocess, REALTIME_PRIORITY_CLASS);
QueryPerformanceFrequency((LARGE_INTEGER*)&freq);
QueryPerformanceCounter((LARGE_INTEGER*)&start);
}
~foo() {
QueryPerformanceCounter((LARGE_INTEGER*)&end);
// SetPriorityClass(hprocess, oldclass);
std::cout << (int)(end - start) << std::endl;
}
};
template <class T>
void swap1(T& a, T& b)
{
T tmp(a);
a = b;
b = tmp;
}
template <class T>
void swap2(T& a, T& b)
{
T tmp(std::move(a));
a = std::move(b);
b = std::move(tmp);
}
int main()
{
static std::string a[N];
for (int i = 0; i < N; i++)
a[i].assign(100000, 'x');
{
std::cout << "Copy Constructor = ";
foo f;
for (int i = 0; i < ITER; i++) {
int x = std::rand() % N;
int y = std::rand() % N;
if (x != y)
swap1(a[x], a[y]);
}
}
{
std::cout << "Move Constructor = ";
foo f;
for (int i = 0; i < ITER; i++) {
int x = std::rand() % N;
int y = std::rand() % N;
if (x != y)
swap2(a[x], a[y]);
}
}
}