// SleepTest.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <windows.h>
#include <chrono>
#include <string>
#include <thread>
#pragma comment(lib,"ws2_32.lib")
#pragma comment(lib,"WinMM.Lib")
using namespace std;
using namespace std::chrono;
#define TEST_TIMES 500
#define BUFFER_SIZE 40960
#define Interval_Microseconds 500
#define Interval_Millisceonds 1
void Precision_Sleep()
{
std::string buffer;
buffer.assign(BUFFER_SIZE, 0);
buffer.clear();
int i = TEST_TIMES;
uint64_t total_used = 0;
// Test for Sleep;
while (i) {
i--;
steady_clock::time_point time_begin = steady_clock::now();
Sleep(Interval_Millisceonds);
steady_clock::time_point time_end = steady_clock::now();
char tmp[128] = {0};
uint64_t used = duration_cast<microseconds>(time_end - time_begin).count();
snprintf(tmp, 128, "%s Sleep %d ms, time used : %lld us\n",
__FUNCTION__, Interval_Millisceonds, used);
total_used += used;
buffer += tmp;
}
printf("%s\n", buffer.c_str());
printf("%s Sleep %d ms, avatar %lld us\n\n",
__FUNCTION__, Interval_Millisceonds,total_used / TEST_TIMES);
}
void Precision_sleep_for()
{
// Test for sleep_for;
std::string buffer;
buffer.assign(BUFFER_SIZE, 0);
buffer.clear();
int i = TEST_TIMES;
uint64_t total_used = 0;
while (i) {
i--;
steady_clock::time_point time_begin = steady_clock::now();
std::this_thread::sleep_for(microseconds(Interval_Microseconds));
steady_clock::time_point time_end = steady_clock::now();
char tmp[128] = {0};
uint64_t used = duration_cast<microseconds>(time_end - time_begin).count();
snprintf(tmp, 128, "%s Sleep %d us, time used : %lld us\n",
__FUNCTION__, Interval_Microseconds, used);
total_used += used;
buffer += tmp;
}
printf("%s", buffer.c_str());
printf("%s Sleep %d us , avatar %lld us\n\n",
__FUNCTION__, Interval_Microseconds, total_used / TEST_TIMES);
}
void Precision_sleep_for_timeBeginPeriod()
{
// Test for sleep_for and timeBeginPeriod;
std::string buffer;
buffer.assign(BUFFER_SIZE, 0);
buffer.clear();
int i = TEST_TIMES;
uint64_t total_used = 0;
while (i) {
i--;
steady_clock::time_point time_begin = steady_clock::now();
timeBeginPeriod(1);//set Minimum timer resolution.
std::this_thread::sleep_for(microseconds(Interval_Microseconds));
timeEndPeriod(1);
steady_clock::time_point time_end = steady_clock::now();
char tmp[128] = {0};
uint64_t used = duration_cast<microseconds>(time_end - time_begin).count();
snprintf(tmp, 128, "%s Sleep %d us , time used : %lld us\n",
__FUNCTION__, Interval_Microseconds, used);
total_used += used;
buffer += tmp;
}
printf("%s", buffer.c_str());
printf("%s Sleep %d us , avatar %lld us\n\n",
__FUNCTION__,Interval_Microseconds, total_used / TEST_TIMES);
}
void SleepSelectUS(SOCKET s, int64_t usec)
{
struct timeval tv;
fd_set dummy;
FD_ZERO(&dummy);
FD_SET(s, &dummy);
tv.tv_sec = usec / 1000000L;
tv.tv_usec = usec % 1000000L;
select(0, 0, 0, &dummy, &tv);
DWORD err = GetLastError();
if (err != 0)
printf("Error : %d", err);
}
void Precision_select()
{
// Test for select;
std::string buffer;
buffer.assign(BUFFER_SIZE, 0);
buffer.clear();
int i = TEST_TIMES;
uint64_t total_used = 0;
WORD wVersionRequested = MAKEWORD(1, 0);
WSADATA wsaData;
WSAStartup(wVersionRequested, &wsaData);
SOCKET s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
while (i) {
i--;
steady_clock::time_point time_begin = steady_clock::now();
SleepSelectUS(s, Interval_Microseconds);
steady_clock::time_point time_end = steady_clock::now();
char tmp[128] = {0};
uint64_t used = duration_cast<microseconds>(time_end - time_begin).count();
snprintf(tmp, 128, "%s Sleep %d us, time used : %lld us\n",__FUNCTION__, Interval_Microseconds, used);
total_used += used;
buffer += tmp;
}
closesocket(s);
printf("%s", buffer.c_str());
printf("%s Sleep %d us, avatar %lld us\n\n", __FUNCTION__, Interval_Microseconds, total_used / TEST_TIMES);
}
void SleepPerformUS(DWORD usec)
{
LARGE_INTEGER perfCnt, start, now;
QueryPerformanceFrequency(&perfCnt);
QueryPerformanceCounter(&start);
do {
QueryPerformanceCounter((LARGE_INTEGER*)&now);
} while ((now.QuadPart - start.QuadPart) / float(perfCnt.QuadPart) * 1000 * 1000 < usec);
}
void Precision_QueryPerformanceCounter()
{
// Test for QueryPerformanceCounter;
std::string buffer;
buffer.assign(BUFFER_SIZE, 0);
buffer.clear();
int i = TEST_TIMES;
uint64_t total_used = 0;
WORD wVersionRequested = MAKEWORD(1, 0);
WSADATA wsaData;
WSAStartup(wVersionRequested, &wsaData);
SOCKET s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
while (i) {
i--;
steady_clock::time_point time_begin = steady_clock::now();
SleepPerformUS(Interval_Microseconds);
steady_clock::time_point time_end = steady_clock::now();
char tmp[128] = {0};
uint64_t used = duration_cast<microseconds>(time_end - time_begin).count();
snprintf(tmp, 128, "%s Sleep %d us, time used : %lld us\n", __FUNCTION__,Interval_Microseconds, used);
total_used += used;
buffer += tmp;
}
closesocket(s);
printf("%s", buffer.c_str());
printf("%s Sleep %d us, avatar %lld us\n\n", __FUNCTION__, Interval_Microseconds,total_used / TEST_TIMES);
}
void Precision_MsgWaitForMultipleObjectsEx()
{
std::string buffer;
buffer.assign(BUFFER_SIZE, 0);
buffer.clear();
int i = TEST_TIMES;
uint64_t total_used = 0;
while (i) {
i--;
steady_clock::time_point time_begin = steady_clock::now();
MsgWaitForMultipleObjectsEx(0, NULL, Interval_Millisceonds, QS_ALLPOSTMESSAGE,
MWMO_INPUTAVAILABLE);
steady_clock::time_point time_end = steady_clock::now();
char tmp[128] = {0};
uint64_t used = duration_cast<microseconds>(time_end - time_begin).count();
snprintf(tmp, 128, "%s Sleep %d ms, time used : %lld us\n", __FUNCTION__, Interval_Millisceonds, used);
total_used += used;
buffer += tmp;
}
printf("%s", buffer.c_str());
printf("%s Sleep %d ms, avatar %lld us\n\n", __FUNCTION__, Interval_Millisceonds, total_used / TEST_TIMES);
}
int main()
{
// Test for Sleep;
//Precision_Sleep();
// Test for sleep_for;
//Precision_sleep_for();
// Test for sleep_for and timeBeginPeriod;
//Precision_sleep_for_timeBeginPeriod();
// Test for select;
Precision_select();
// Test for QueryPerformanceCounter;
//Precision_QueryPerformanceCounter();
// Test for MsgWaitForMultipleObjectsEx;
//Precision_MsgWaitForMultipleObjectsEx();
system("pause");
return 0;
}