Zeit bei open MP messen



  • Hi,
    ich möchte wissen wie lange mein Programm läuft.
    Als Beispiel habe ich das modifiziert:
    http://www.cplusplus.com/reference/ctime/clock/

    /* clock example: frequency of primes */
    #include <stdio.h>      /* printf */
    #include <time.h>       /* clock_t, clock, CLOCKS_PER_SEC */
    #include <math.h>       /* sqrt */
    #include <omp.h>
    
    int frequency_of_primes (int n) {
        int i,j;
        int freq=n-1;
        for (i=2; i<=n; ++i) for (j=sqrt(i);j>1;--j) if (i%j==0) {--freq; break;}
        return freq;
    }
    
    int main ()
    {
        int max = 1;    
        #ifdef _OPENMP
        max = omp_get_max_threads();
        #endif
    
        int f;
        for(int tn=1; tn<=max; tn++){
            clock_t t;
            float allTime = 0;
            f=0;
    
            int threads = tn;
            #pragma omp parallel for private(t) reduction(+:f,allTime) num_threads(threads)
            for(int i = 0; i<10000; i++){
                t = clock();    
                f += frequency_of_primes (i);
                t = clock() - t;
                float ct = ((float)t/CLOCKS_PER_SEC);
    
                allTime+= ct;
    
            }
            printf ("It took me %f seconds (%i threads).\n", allTime, threads);
        }
        printf ("The sum of the amount of primes from 0 to 0 up to 10,000 is: %d\n",f);
        return 0;
    }
    

    raus kommt:

    It took me 7.034076 seconds (1 threads).
    It took me 9.627321 seconds (2 threads).
    It took me 13.447388 seconds (3 threads).
    It took me 17.541000 seconds (4 threads).
    The sum of the amount of primes from 0 to 0-10,000 is: 6553603
    

    clock ist ja nicht die richtige Zeit sondern eher die Rechenzeit. Meine Frage ist nun warum die bei mehreren Threads so viel größer wird. Oder kann man clock bei mehr als einen Thread nicht mehr verwenden?
    So würde es ja heißen, dass bei 4 threads mehr als doppelt soviele Rechenschritte gemacht wurden als wie bei einem, obwohl das selbe ausgerechnet wurde.

    Gibt es da eine andere Möglichkeit die Rechenzeit auszurechnen?



  • Oder wie kann ich herausfinden wieviel fach schneller mein Programm mit threads läuft auf einem PC auf dem auch noch andere Dinge nebenbei laufen.



  • Unter Windows gibt es die Funktion queryperformancecounter


Anmelden zum Antworten