Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2013
515
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.44 KB | None | 0 0
  1. #include <windows.h>
  2.  
  3. #include <intrin.h>
  4.  
  5. #include <iostream>
  6. #include <vector>
  7. #include <boost/date_time/posix_time/posix_time.hpp>
  8. using namespace std;
  9.  
  10. void realtime(bool on = true)
  11. {
  12.     if (on) {
  13.         HANDLE TokenHandle = NULL;
  14.         if (OpenProcessToken(GetCurrentProcess(), (TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES), &TokenHandle) == FALSE) throw runtime_error("OpenProcessToken");
  15.         LUID luid = {0};
  16.         if (LookupPrivilegeValue(NULL, SE_INC_BASE_PRIORITY_NAME, &luid) == FALSE) throw runtime_error("LookupPrivilegeValue");
  17.         TOKEN_PRIVILEGES tkn = {1, {luid, SE_PRIVILEGE_ENABLED}};
  18.         AdjustTokenPrivileges(TokenHandle, FALSE, &tkn, sizeof(TOKEN_PRIVILEGES), NULL, NULL);
  19.         if (ERROR_NOT_ALL_ASSIGNED == GetLastError()) throw runtime_error("AdjustTokenPrivileges");
  20.         if (SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS) == FALSE) throw runtime_error("SetPriorityClass");
  21.         if (SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL) == FALSE) throw runtime_error("SetThreadPriority");
  22.         CloseHandle(TokenHandle);
  23.     } else {
  24.         if (SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS) == FALSE) throw runtime_error("SetPriorityClass");
  25.         if (SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL) == FALSE) throw runtime_error("SetThreadPriority");
  26.     }
  27. }
  28.  
  29. _forceinline unsigned long long rdtsc()
  30. {
  31.     __asm {
  32.         rdtsc
  33.     }
  34. }
  35.  
  36. struct benchmark
  37. {
  38.     bool is_realtime;
  39.     benchmark() : is_realtime(true) { try { realtime(); } catch (const runtime_error&) { is_realtime = false; } }
  40.     ~benchmark() { realtime(false); }
  41. };
  42.  
  43. struct measure
  44. {
  45.     boost::posix_time::ptime t0, t1;
  46.     unsigned long long ts0, ts1;
  47.     measure() {
  48.         t0 = boost::date_time::microsec_clock<boost::posix_time::ptime>::local_time();
  49.         ts0 = rdtsc();
  50.     }
  51.     ~measure() {
  52.         ts1 = rdtsc();
  53.         t1 = boost::date_time::microsec_clock<boost::posix_time::ptime>::local_time();
  54.         try {
  55.             cout << t1 - t0 << " (" << ts1 - ts0 << " tacts, estimated CPU freq " << double(ts1 - ts0) / (t1 - t0).total_microseconds() << " MHz" << endl;
  56.         } catch (...) { }
  57.     }
  58. };
  59.  
  60.  
  61. int main()
  62. {
  63.     map<int, int> res;
  64.     { benchmark b;
  65.         for (int i = 100000; i <= 10000000; i += 100000) {
  66.             measure m;
  67.             int s = 0;
  68.             for (int j = i; j < i * 2; ++j) {
  69.                 s += j;
  70.             }
  71.             res[i] = s;
  72.         }
  73.         cout << "Benchmark realtime: " << boolalpha << b.is_realtime << endl;
  74.     }
  75.     for (auto i = res.begin(), iend = res.end(); i != iend; ++i) {
  76.         cout << i->second << endl;
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement