Advertisement
Guest User

Untitled

a guest
Feb 28th, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #include <windows.h>
  4. #include <winbase.h>
  5. #include <winnt.h>
  6. #include <vector>
  7.  
  8. bool is_prime(int n) {
  9.     const int limit = (int)sqrt((float)n)+1;
  10.  
  11.     for (int i = 2; i < limit; i++) {
  12.         if (n%i == 0) {
  13.             return false;
  14.         }
  15.     }
  16.  
  17.     return true;
  18. }
  19.  
  20. int main() {
  21.     std::vector<float> times;
  22.  
  23.     LARGE_INTEGER frequency;
  24.     frequency.QuadPart = 0;
  25.     QueryPerformanceFrequency(&frequency);
  26.  
  27.     for (int i = 0; i < 50; i++) {
  28.         int num = 0;
  29.         int primes_found = 0;
  30.         LARGE_INTEGER begin_time;
  31.         LARGE_INTEGER end_time;
  32.  
  33.         begin_time.QuadPart = 0;
  34.         end_time.QuadPart = 0;
  35.  
  36.         QueryPerformanceCounter(&begin_time);
  37.         while (primes_found != 10000) {
  38.             if (is_prime(num++)) {
  39.                 primes_found++;
  40.             }
  41.         }
  42.         QueryPerformanceCounter(&end_time);
  43.    
  44.         LARGE_INTEGER difference;
  45.         difference.QuadPart = (end_time.QuadPart - begin_time.QuadPart);
  46.  
  47.         times.push_back(((float)difference.QuadPart/((float)frequency.QuadPart/1000.0f)));
  48.     }
  49.  
  50.     float sum = 0.0f;
  51.     for (std::vector<float>::iterator itr = times.begin(); itr != times.end(); itr++) {
  52.         sum += *itr;
  53.     }
  54.  
  55.     printf("Average Time: %fms\n", sum/(float)times.size());
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement