fork download
#include <cstdio>
#include <iostream>
#include <map>
#include <limits>

#include "windows.h"

using namespace std;

int main()
{
	LARGE_INTEGER tmp_buffer;

	double freq;
	double ns_per_tick;

	if (!QueryPerformanceFrequency(&tmp_buffer))
		cout << "QueryPerformanceFrequency failed\n"; 

	freq = (double)tmp_buffer.QuadPart;
	ns_per_tick = 1E9 / freq;
	cout << "cpu freq = " << ns_per_tick << "ns" << endl;

	int count = 1000 * 1000;
	map<__int64, int> ccnt;
	__int64 ft_wait;
	__int64 min_wait = LLONG_MAX;
	__int64 max_wait = LLONG_MIN;

	cout << "Wait time (ns): ";
	cin >> ft_wait;
	ft_wait /= ns_per_tick;

	for (size_t i = 0; i < count; i++)
	{
		__int64 ft_beg, ft_end;
		__int64 ft_current;

		QueryPerformanceCounter(&tmp_buffer);
		ft_beg = tmp_buffer.QuadPart;

		do
		{
			QueryPerformanceCounter(&tmp_buffer);
			ft_current = tmp_buffer.QuadPart;
		} while ((ft_current - ft_beg) < ft_wait);

		ft_end = ft_current;
		__int64 ft_real_wait = ft_end - ft_beg;
		
		min_wait = min_wait > ft_real_wait ? ft_real_wait : min_wait;
		max_wait = max_wait < ft_real_wait ? ft_real_wait : max_wait;

		if (ccnt.find(ft_real_wait) != ccnt.end())
			ccnt[ft_real_wait]++;
		else
			ccnt.insert(pair<__int64, int>(ft_real_wait, 1));
	}

	for (auto i = ccnt.begin(); i != ccnt.end(); ++i)
		cout << "計時長度" << (int)(i->first * ns_per_tick) << "發生 " << i->second << " 次" << endl;

	cout << "計數次數" << count << " 次" << endl;
	cout << "最小計時長度" << min_wait * ns_per_tick << "ns" << endl;
	cout << "最大計時長度" << max_wait * ns_per_tick << "ns" << endl;

	getchar();
	getchar();
}
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:6:10: fatal error: windows.h: No such file or directory
 #include "windows.h"
          ^~~~~~~~~~~
compilation terminated.
stdout
Standard output is empty