2011年9月20日 星期二

real time, user time, sys time of a process

針對一個process的時間統計:

real==>指逝去的時間(elapsed time)(time of waiting for I/O to complete + time used by other processes)(透過gettimeofday()或times()收集)
user==>指CPU執行該process的user-mode code的時間(透過 times()收集)
sys==>指CPU執行該process的kernel-mode code的時間(透過times()收集)
User+Sys==>指CPU真正執行該procees的時間(可透clock()收集)

http://www.gnu.org/s/hello/manual/libc/CPU-Time.html#CPU-Time
http://osr600doc.sco.com/en/SDK_c++/_Measuring_Program_Execution_Tim.html


#include stdio.h
#include time.h

#include sys/param.h
#include sys/times.h
#include sys/types.h

int main(){
int i;
struct tms t,u;
long r1,r2;
struct timeval start, end;
clock_t start_c, end_c;
gettimeofday(&start, NULL);
r1 = times(&t);
start_c=clock();
for(i=0;i < 10000 ;i++){
printf("hello!!\n");
}
end_c=clock();
r2 = times(&u);
gettimeofday(&end, NULL);
printf("\nHZ=%d\n",HZ);
printf("user time=%f\n",((float)(u.tms_utime-t.tms_utime))/(HZ));
printf("system time=%f\n",((float)(u.tms_stime-t.tms_stime))/(HZ));
printf("real time=%f\n",((float)(r2-r1))/(HZ));
printf("real time(gettimeofday())=%f\n", (float)((end.tv_sec * 1000000 + end.tv_usec)- (start.tv_sec * 1000000 + start.tv_usec))/1000000);
printf("user+real=%f\n" ,((double) (end_c - start_c)) / CLOCKS_PER_SEC);
return 0;
}


output:
hello!!
.....
hello!!

HZ=100
user time=0.000000
system time=0.020000
real time=0.150000
real time(gettimeofday())=0.151931
user+real=0.020000


clock()的實作:

#include sys/times.h
#include sys/param.h
#include time.h
#include unistd.h

clock_t clock(void) {
struct tms buf;
times(&buf);



#if CLOCKS_PER_SEC == HZ
return (unsigned long) buf.tms_utime + buf.tms_stime;
#elif CLOCKS_PER_SEC % HZ == 0
return ((unsigned long) buf.tms_utime + buf.tms_stime) * (CLOCKS_PER_SEC / HZ);
#elif HZ % CLOCKS_PER_SEC == 0
return ((unsigned long) buf.tms_utime + buf.tms_stime) / (HZ / CLOCKS_PER_SEC);
#else
return ((unsigned long long) buf.tms_utime + buf.tms_stime) * CLOCKS_PER_SEC / HZ;
#endif
}

11 則留言:

匿名 提到...

to buy cUTuSYQy [URL=http://www.camera--lenses.com/]canon ef 75-300mm[/URL] at my estore gsjlmsBt [URL=http://www.camera--lenses.com/ ] http://www.camera--lenses.com/ [/URL]

匿名 提到...

[url=http://www.onlinecasinos.gd]casino[/url], also known as effective casinos or Internet casinos, are online versions of respected ("chunk and mortar") casinos. Online casinos substantiate gamblers to reform and wager on casino games from fore-part to foot the Internet.
Online casinos typically submit on the booster evil odds and payback percentages that are comparable to land-based casinos. Some online casinos contend higher payback percentages in the amenities of m‚storey automobile games, and some convert known payout consequence profit audits on their websites. Assuming that the online casino is using an aptly programmed unsystematic take the measure of c estimate a reiterate up generator, recount games like blackjack coveted an established train edge. The payout shard after these games are established sooner than the rules of the game.
Assorted online casinos confirm minus or earn their software from companies like Microgaming, Realtime Gaming, Playtech, Worldwide Skilfulness Technology and CryptoLogic Inc.

匿名 提到...

top [url=http://www.c-online-casino.co.uk/]uk bonus casino[/url] coincide the latest [url=http://www.realcazinoz.com/]free casino[/url] unshackled no set aside bonus at the leading [url=http://www.baywatchcasino.com/]bay take note of casino
[/url].

匿名 提到...

top [url=http://www.c-online-casino.co.uk/]casino games[/url] hinder the latest [url=http://www.casinolasvegass.com/]online casino[/url] manumitted no deposit bonus at the foremost [url=http://www.baywatchcasino.com/]online casino
[/url].

匿名 提到...

best for you LjxQxZKr [URL=http://www.cheapguccireplica.tumblr.com/]outlet gucci[/URL] , just clicks away JPIJAsjj [URL=http://www.cheapguccireplica.tumblr.com/ ] http://www.cheapguccireplica.tumblr.com/ [/URL]

匿名 提到...

cheap hcMJUznH [URL=http://www.louisvuittonoutletshop.tumblr.com/ - outlet louis vuitton[/URL - for promotion code hPmYUOGJ [URL=http://www.louisvuittonoutletshop.tumblr.com/ - http://www.louisvuittonoutletshop.tumblr.com/ [/URL -

匿名 提到...

order an mFRgsdrg [URL=http://www.aaareplicahandbags.weebly.com/]aaa replica[/URL] for more detail RrvHmvnV [URL=http://www.aaareplicahandbags.weebly.com/ ] http://www.aaareplicahandbags.weebly.com/ [/URL]

匿名 提到...

beverly ma dating http://loveepicentre.com/taketour/ free christian online dating websites
who is bono dating [url=http://loveepicentre.com/map/]dating show you're interested[/url] us dating site teen
dating profile how [url=http://loveepicentre.com]swinger married dating[/url] philipna dating [url=http://loveepicentre.com/user/Megand/]Megand[/url] bbw dating ontario

匿名 提到...

dating methods for rocks http://loveepicentre.com free foot fetish dating sites
love happens dating site [url=http://loveepicentre.com/advice/]cool dating magazines[/url] muslim dating cultures
final fantasy sim dating walkthrough [url=http://loveepicentre.com/articles/]romantic things for christian dating[/url] adult dating in carlow [url=http://loveepicentre.com/user/vbabydavid/]vbabydavid[/url] russian dating pocket money scam

匿名 提到...

dating severis http://loveepicentre.com/contact/ heidi cornell is she dating bill
koreans single dating ladies [url=http://loveepicentre.com/success_stories/]dating game spoof[/url] dating china marriages
dating game show theme title [url=http://loveepicentre.com/articles/]which dating sites show weight[/url] asian international dating vietnam [url=http://loveepicentre.com/user/sweetann/]sweetann[/url] damien fahey dating lindsay lohan

匿名 提到...

free dating san francisco bay area http://loveepicentre.com/articles/ erotic phone dating philadelphia
dating initial contact [url=http://loveepicentre.com/articles/]dating escort cedar rapids iowa[/url] dating tips sydney australia
idaho dating and personals [url=http://loveepicentre.com/]dating websites lure to join[/url] new dating sites free [url=http://loveepicentre.com/user/lonelyworld1/]lonelyworld1[/url] survey on internet dating