0% found this document useful (0 votes)
74 views6 pages

How To Measure The Time in Multi

The document discusses measuring elapsed time in multi-threaded programs running on multi-core machines. It explains that the clock() function cannot be used to determine real elapsed time as it only measures CPU time. It then asks how to measure the exact time elapsed instead of an approximation in seconds returned by time(). The code sample provided creates threads that perform random number generation operations and measures start and end times to calculate duration, but the results are still an approximation.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views6 pages

How To Measure The Time in Multi

The document discusses measuring elapsed time in multi-threaded programs running on multi-core machines. It explains that the clock() function cannot be used to determine real elapsed time as it only measures CPU time. It then asks how to measure the exact time elapsed instead of an approximation in seconds returned by time(). The code sample provided creates threads that perform random number generation operations and measures start and end times to calculate duration, but the results are still an approximation.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

How to measure the time in multi-core machines with pthreads?

After spending about an hour trying to figure out what was the problem that my threaded version of a program took more time than the single-core one, I think I rediscovered the wheel. Simply put, I can not use the clock () function to determine the "real" time I spent working in parallel. What do I mean !"ample# A program creates some threads and each one of them will make argv$%& calls to the rand '( function ')ust to do something so that time goes by (. *hen in the end I compute the time recorded by clock '( differences and differences with time'(. +bviously the ratio below is almost two because my machine is dual core. ,ut ... Question: -ow can I measure the e"act time elapsed and not the appro"imation in seconds with time'( .ode#
To plain text

1 2 3 4 5 6 7 8 9 10

$ g++ try_to_time.cpp -pthread -o try $ ./try 50000000 Just started counting! All threads joined! Duration ! ."#"" secs.

Duration $! % started ! % )inished! $0&"'###( seconds a)ter $0&"'##55 seconds a)ter / / (*0. / / (*0.

Duration '! & secs. $

And the program

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

$ cat try_to_time.cpp +include ,iostream+include ,iomanip+include ,cstdli.+include ,ctime+include ,pthread.h-

using namespace std/

+de)ine 012_3456AD7 +de)ine D89837 #

:oid % thread_)unction ;:oid %</

int main ;int argc= char % arg: >?< @

17 18 19 20

time_t start = start$= start'/ time_t end = end$= end'/ int iterations= i= rc/

21 22 23 24 25 26 27 28 29 30 31 32 33 34

:oid % status/ dou.le duration/

i) ;argc !A $< @ cout ,, B1sage error. 8 am eCpecting one positi:e integer.B ,, endl/ eCit ; </ D iterations A atoi ; arg: > ? </

// create opaEue o.jects. pthread_t % threads A neF pthread_t >012_3456AD7?/ // 2aGe threads Joina.le )or sure. pthread_attr_t attr/ pthread_attr_init ;Hattr</

35 36 37 38 39 40

pthread_attr_setdetachstate ;Hattr= I3456AD_J56A36_JK80ALM6</

start

A clocG ;</

start$ A time ;01MM</ time ;Hstart'</

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

cout ,, BJust started counting!B ,, std!!endl/

)or ;i A 0/ i , 012_3456AD7/ i++< @ rc A pthread_create;H;threads>i?<= Hattr= thread_)unction= ;:oid %< Hiterations</ i) ;rc< @ cerr ,, B655K5/ return code )rom pthread_create;< is B ,, rc ,, std!!endl/ eCit ;$</ D D

)or ;i A 0/ i , 012_3456AD7/ i++< pthread_join ; threads >i?= Hstatus </ cout ,, BAll threads joined!B ,, std!!endl/

end
57 58 59 60

A clocG ;</

end$ A time ;01MM</ time ;Hend'</

61 62 63 64 65 66 67 68 69 70 71 72 73 74

duration A static_cast,dou.le- ;end

- start </

i) ;duration , static_cast,dou.le- ;JMKJN7_I65_76J<< @ cout ,, BOeF iterations. 3ry again Fith a .igger num.er.B ,, endl/ eCit ;'</ D

cout ,, BDuration

! B ,, )iCed ,, setprecision ;D89837<

,, static_cast,dou.le- ;duration / JMKJN7_I65_76J< ,, B secs.B ,, endl/ cout ,, BDuration $! B ,, endl/ cout ,, B% started ! B ,, start$ ,, B seconds a)ter cout ,, B% )inished! B ,, end$ ,, B seconds a)ter / / (*0.B ,, endl/ / / (*0.B ,, endl/

cout ,, BDuration '! B ,, static_cast,int- ;di))time ; end'= start' < < ,, B secs.B ,, endl/

return 0/
75 76 77 78 79 80

:oid % thread_)unction ;:oid % input< @ int loops A %;;int %< input</

int i/

81 82 83

)or ;i A 0/ i , loops/ i++< rand ;< H+'*/ 5A0D_2AP/ // Just a dummy operation.

pthread_eCit ;01MM</ D

You might also like