Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <math.h>
- #include <windows.h>
- #include <winbase.h>
- #include <winnt.h>
- #include <vector>
- bool is_prime(int n) {
- const int limit = (int)sqrt((float)n)+1;
- for (int i = 2; i < limit; i++) {
- if (n%i == 0) {
- return false;
- }
- }
- return true;
- }
- int main() {
- std::vector<float> times;
- LARGE_INTEGER frequency;
- frequency.QuadPart = 0;
- QueryPerformanceFrequency(&frequency);
- for (int i = 0; i < 50; i++) {
- int num = 0;
- int primes_found = 0;
- LARGE_INTEGER begin_time;
- LARGE_INTEGER end_time;
- begin_time.QuadPart = 0;
- end_time.QuadPart = 0;
- QueryPerformanceCounter(&begin_time);
- while (primes_found != 10000) {
- if (is_prime(num++)) {
- primes_found++;
- }
- }
- QueryPerformanceCounter(&end_time);
- LARGE_INTEGER difference;
- difference.QuadPart = (end_time.QuadPart - begin_time.QuadPart);
- times.push_back(((float)difference.QuadPart/((float)frequency.QuadPart/1000.0f)));
- }
- float sum = 0.0f;
- for (std::vector<float>::iterator itr = times.begin(); itr != times.end(); itr++) {
- sum += *itr;
- }
- printf("Average Time: %fms\n", sum/(float)times.size());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement