Advertisement
ritwik876

ForHoward

Jul 6th, 2014
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.76 KB | None | 0 0
  1. Version One:
  2.  
  3. #include <iostream>
  4. #include <thread>
  5. #include <chrono>
  6. #include <vector>
  7. #include <algorithm>
  8.  
  9. // g++ -std=c++11 -O3 -s -DNDEBUG chrono.cpp
  10.  
  11. int main()
  12. {
  13.     typedef std::chrono::high_resolution_clock Clock;
  14.     typedef std::chrono::nanoseconds nanoseconds;
  15.  
  16.     std::vector<int> v;
  17.     v.push_back(45); v.push_back(56); v.push_back(57); v.push_back(58); v.push_back(59); v.push_back(59);
  18.     v.push_back(61); v.push_back(62); v.push_back(62); v.push_back(63); v.push_back(64); v.push_back(65);
  19.     const int N = 100;
  20.  
  21.     Clock::time_point t0 = Clock::now();
  22.     //Test area
  23.     for (int j = 0; j < N; ++j)
  24.     {
  25.         v.push_back(j);
  26.         std::sort(v.begin(), v.end());
  27.     }
  28.     //std::this_thread::sleep_for(nanoseconds(50));
  29.    
  30.  
  31.     Clock::time_point t1 = Clock::now();
  32.    
  33.     nanoseconds ms = std::chrono::duration_cast<nanoseconds>(t1 - t0)/N;
  34.     std::cout << ms.count() << " Nanoseconds\n";
  35. }
  36.  
  37. Results(multiple runs):
  38. 1893 Nanoseconds
  39.  
  40. 5025 Nanoseconds
  41.  
  42. 5035 Nanoseconds
  43.  
  44. 5039 Nanoseconds
  45.  
  46. 1897 Nanoseconds
  47.  
  48.  
  49. 6961 Nanoseconds
  50.  
  51. 5039 Nanoseconds
  52.  
  53. 6046 Nanoseconds
  54.  
  55. ----------------------------------------------------------------------------------------------------
  56.  
  57. Version 2:
  58.  
  59. #include <chrono>
  60. #include <iostream>
  61. #include <vector>
  62. #include <algorithm>
  63.  
  64. namespace x
  65. {
  66.  
  67. struct clock
  68. {
  69.     typedef unsigned long long                 rep;
  70.     typedef std::ratio<1, 1600000000>          period; // My machine is 2.8 GHz
  71.     typedef std::chrono::duration<rep, period> duration;
  72.     typedef std::chrono::time_point<clock>     time_point;
  73.     static const bool is_steady =              true;
  74.  
  75.     static time_point now() noexcept
  76.     {
  77.         unsigned lo, hi;
  78.         asm volatile("rdtsc" : "=a" (lo), "=d" (hi));
  79.         return time_point(duration(static_cast<rep>(hi) << 32 | lo));
  80.     }
  81. };
  82.  
  83. }  // x
  84.  
  85. template <class clock>
  86. void
  87. test_empty_loop()
  88. {
  89.     // Define real time units
  90.     typedef std::chrono::duration<unsigned long long, std::pico> picoseconds;
  91.     // or:
  92.     // typedef std::chrono::nanoseconds nanoseconds;
  93.     // Define double-based unit of clock tick
  94.     typedef std::chrono::duration<double, typename clock::period> Cycle;
  95.     using std::chrono::duration_cast;
  96.     const int N = 100;
  97.     // Do it
  98.  
  99.  
  100.     std::vector<int> v;
  101.     v.push_back(45); v.push_back(56); v.push_back(57); v.push_back(58); v.push_back(59); v.push_back(59);
  102.     v.push_back(61); v.push_back(62); v.push_back(62); v.push_back(63); v.push_back(64); v.push_back(65);
  103.  
  104.     auto t0 = clock::now();
  105.  
  106.     /*Test area begins*/
  107.  
  108.     // for (int j = 0; j < N; ++j)
  109.     //     asm volatile("");
  110.     for (int j = 0; j < N; ++j)
  111.     {
  112.         v.push_back(j);
  113.         std::sort(v.begin(), v.end());
  114.     }
  115.     /*Test area ends*/
  116.  
  117.     auto t1 = clock::now();
  118.     // Get the clock ticks per iteration
  119.     auto ticks_per_iter = Cycle(t1-t0)/N;
  120.     std::cout << ticks_per_iter.count() << " clock ticks per iteration\n";
  121.     // Convert to real time units
  122.     std::cout << duration_cast<picoseconds>(ticks_per_iter).count()
  123.               << "ps per iteration\n";
  124. }
  125.  
  126. int main()
  127. {
  128.     std::cout << "Precision: "<<std::chrono::high_resolution_clock::period::den << std::endl;
  129.  
  130.     std::cout << "\nUsing rdtsc:\n";
  131.     test_empty_loop<x::clock>();
  132.  
  133.     std::cout << "\nUsing std::chrono::high_resolution_clock:\n";
  134.     test_empty_loop<std::chrono::high_resolution_clock>();
  135.  
  136.     std::cout << "\nUsing std::chrono::system_clock:\n";
  137.     test_empty_loop<std::chrono::system_clock>();
  138.  
  139.    
  140. }
  141.  
  142. Results:
  143.  
  144.  
  145. Precision: 1000000000
  146.  
  147. Using rdtsc:
  148. 7862.72 clock ticks per iteration
  149. 4914200ps per iteration
  150.  
  151. Using std::chrono::high_resolution_clock:
  152. 4932.74 clock ticks per iteration
  153. 4932739ps per iteration
  154.  
  155. Using std::chrono::system_clock:
  156. 5665.97 clock ticks per iteration
  157. 5665970ps per iteration
  158.  
  159.  
  160. Another run:
  161.  
  162. Precision: 1000000000
  163.  
  164. Using rdtsc:
  165. 11240.3 clock ticks per iteration
  166. 7025199ps per iteration
  167.  
  168. Using std::chrono::high_resolution_clock:
  169. 5014.35 clock ticks per iteration
  170. 5014350ps per iteration
  171.  
  172. Using std::chrono::system_clock:
  173. 7627.23 clock ticks per iteration
  174. 7627229ps per iteration
  175.  
  176. Another run:
  177. Precision: 1000000000
  178.  
  179. Using rdtsc:
  180. 7875.04 clock ticks per iteration
  181. 4921899ps per iteration
  182.  
  183. Using std::chrono::high_resolution_clock:
  184. 5890.23 clock ticks per iteration
  185. 5890229ps per iteration
  186.  
  187. Using std::chrono::system_clock:
  188. 5641.11 clock ticks per iteration
  189. 5641109ps per iteration
  190.  
  191. Another run:
  192.  
  193. Using rdtsc:
  194. 3714.96 clock ticks per iteration
  195. 2321850ps per iteration
  196.  
  197. Using std::chrono::high_resolution_clock:
  198. 1874.76 clock ticks per iteration
  199. 1874759ps per iteration
  200.  
  201. Using std::chrono::system_clock:
  202. 1832.86 clock ticks per iteration
  203. 1832859ps per iteration
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement