lundi 3 juin 2019

C++ high_resolution_clock::now() showing inconsistent results with wallclock time

I was trying to calculate the time required to sample 7680 bit primes in FLINT library. I had a loop running for 100 iterations and finally calculated the average time required. On my mac, the below code took more than 5 hours to run (I left the code running in mac at 2:00clock without closing the lid. When I saw again at 7:00clock, the code is still running). But finally, it showed an output of "33.3442" seconds. How is this possible?

#include "fmpz.h"
#include <iostream>
#include <chrono>
using namespace std;
using namespace std::chrono;

int main() {  
    int count = 100;
    int length = 7680;

    fmpz_t primes[count];   
    flint_rand_t state;
    flint_randinit(state);

    for (int i = 0; i < count; i++)
        fmpz_init(primes[i]);


    auto start = high_resolution_clock::now(); 
    for (int i = 0; i < count; i++)
    {
        while (true)
        {
             fmpz_randbits(primes[i], state, length);
             if (fmpz_is_probabprime(primes[i]))
                  break;
        }
    }
    auto stop = high_resolution_clock::now(); 
    auto duration = duration_cast<microseconds>(stop - start); 
    cout << "Generating random primes of length " << length << " " << ((double)duration.count()/1000000)/count << endl; 
}

Aucun commentaire:

Enregistrer un commentaire