Advertisement
AI_UBI

Untitled

Dec 28th, 2015
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <chrono>
  4.  
  5. typedef std::chrono::high_resolution_clock Clock;
  6.  
  7. unsigned check(int *Items, int & Count)
  8. {
  9.     int max = 0, start = 0, end = 0;
  10.  
  11.     while (Count--)
  12.     {
  13.         if (Items[Count] > max)
  14.         {
  15.             max = Items[Count];
  16.             start = end = Count;
  17.         }
  18.         else if (Items[Count] == max) end = Count;
  19.     }
  20.  
  21.     return start == end ? 0 : (start - end - 1);
  22. }
  23.  
  24. void test()
  25. {
  26.     int iterations = 1000000, cur = 0, count = 20;
  27.     int items[20] = { 1, -1, 30, 44, 55, 44, 22, 1, 0, -12, 33, 95, 0, 2, 95, -10, 95, 33, 22, 95 };
  28.     std::chrono::time_point<std::chrono::steady_clock> before, after;
  29.     long float AVG = 0;
  30.    
  31.     while (cur < iterations)
  32.     {
  33.         before = Clock::now();
  34.  
  35.         check(items, count = 20);
  36.  
  37.         after = Clock::now();
  38.        
  39.         AVG += std::chrono::duration_cast<std::chrono::nanoseconds>(after-before).count();
  40.  
  41.         ++cur;
  42.     }
  43.    
  44.     printf("AVG time per %d iterations.\nIn nanoseconds\nTotal: %f\nAvg: %f \n", iterations, AVG, AVG / iterations);
  45. }
  46.  
  47. int main()
  48. {
  49.     test();
  50.     getchar();
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement