Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <intrin.h>
- #include <iostream>
- #include <vector>
- #include <boost/date_time/posix_time/posix_time.hpp>
- using namespace std;
- void realtime(bool on = true)
- {
- if (on) {
- HANDLE TokenHandle = NULL;
- if (OpenProcessToken(GetCurrentProcess(), (TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES), &TokenHandle) == FALSE) throw runtime_error("OpenProcessToken");
- LUID luid = {0};
- if (LookupPrivilegeValue(NULL, SE_INC_BASE_PRIORITY_NAME, &luid) == FALSE) throw runtime_error("LookupPrivilegeValue");
- TOKEN_PRIVILEGES tkn = {1, {luid, SE_PRIVILEGE_ENABLED}};
- AdjustTokenPrivileges(TokenHandle, FALSE, &tkn, sizeof(TOKEN_PRIVILEGES), NULL, NULL);
- if (ERROR_NOT_ALL_ASSIGNED == GetLastError()) throw runtime_error("AdjustTokenPrivileges");
- if (SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS) == FALSE) throw runtime_error("SetPriorityClass");
- if (SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL) == FALSE) throw runtime_error("SetThreadPriority");
- CloseHandle(TokenHandle);
- } else {
- if (SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS) == FALSE) throw runtime_error("SetPriorityClass");
- if (SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL) == FALSE) throw runtime_error("SetThreadPriority");
- }
- }
- _forceinline unsigned long long rdtsc()
- {
- __asm {
- rdtsc
- }
- }
- struct benchmark
- {
- bool is_realtime;
- benchmark() : is_realtime(true) { try { realtime(); } catch (const runtime_error&) { is_realtime = false; } }
- ~benchmark() { realtime(false); }
- };
- struct measure
- {
- boost::posix_time::ptime t0, t1;
- unsigned long long ts0, ts1;
- measure() {
- t0 = boost::date_time::microsec_clock<boost::posix_time::ptime>::local_time();
- ts0 = rdtsc();
- }
- ~measure() {
- ts1 = rdtsc();
- t1 = boost::date_time::microsec_clock<boost::posix_time::ptime>::local_time();
- try {
- cout << t1 - t0 << " (" << ts1 - ts0 << " tacts, estimated CPU freq " << double(ts1 - ts0) / (t1 - t0).total_microseconds() << " MHz" << endl;
- } catch (...) { }
- }
- };
- int main()
- {
- map<int, int> res;
- { benchmark b;
- for (int i = 100000; i <= 10000000; i += 100000) {
- measure m;
- int s = 0;
- for (int j = i; j < i * 2; ++j) {
- s += j;
- }
- res[i] = s;
- }
- cout << "Benchmark realtime: " << boolalpha << b.is_realtime << endl;
- }
- for (auto i = res.begin(), iend = res.end(); i != iend; ++i) {
- cout << i->second << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement