delorie.com/archives/browse.cgi | search |
From: | Alex Vinokur <alexvn AT bigfoot DOT com> |
Newsgroups: | comp.os.msdos.djgpp |
Subject: | uclock() and struct rusage |
Date: | Tue, 27 Nov 2001 10:35:12 +0200 |
Organization: | Scopus Network Technologies |
Lines: | 194 |
Message-ID: | <3C03503F.E9AE61D3@bigfoot.com> |
NNTP-Posting-Host: | 62.90.123.5 |
Mime-Version: | 1.0 |
X-Trace: | fu-berlin.de 1006850137 5384086 62.90.123.5 (16 [79865]) |
X-Mailer: | Mozilla 4.7 [en] (Win98; I) |
X-Accept-Language: | en |
To: | djgpp AT delorie DOT com |
DJ-Gateway: | from newsgroup comp.os.msdos.djgpp |
Reply-To: | djgpp AT delorie DOT com |
=============================================================== Windows98 gpp : GNU C++ version 2.95.3 20010315/djgpp (release) (djgpp) compiled by GNU C version 2.95.3 20010315/djgpp (release). =============================================================== It seems that it is preferable to use uclock() instead of 'struct rusage' to get user time used by process Resolution of uclock() is higher than 'struct rusage'. UCLOCKS_PER_SEC is 1193180 // in time.h See a test below. Wee can we that sometimes 'struct rusage' returns 0 sec (when used time is too small). Nevertheless, is there any situations that it is worth using 'struct rusage' instead uclock()? Answer#1. uclock() is non portable. Something else ? //------------- C++ Code : BEGIN ------------- #include <iostream> #include <sys/time.h> #include <sys/resource.h> #include <utime.h> //------------------------- uclock_t start_uclock_time; uclock_t end_uclock_time; struct rusage start_rusage_time; struct rusage end_rusage_time; //------------------------- void set_start_time() { start_uclock_time = uclock(); getrusage(RUSAGE_SELF, &start_rusage_time); } //------------------------- void set_end_time() { end_uclock_time = uclock(); getrusage(RUSAGE_SELF, &end_rusage_time); } //------------------------- void show_user_used_time() { uclock_t used_uclock_time = end_uclock_time - start_uclock_time; cout << "User time used : " << used_uclock_time << " uclocks" << endl; double user_rusage_time = (end_rusage_time.ru_utime.tv_sec - start_rusage_time.ru_utime.tv_sec) + (end_rusage_time.ru_utime.tv_usec - start_rusage_time.ru_utime.tv_usec)/ 1.0e6; cout << "User time used : " << user_rusage_time << " sec" << endl; cout << endl; } //------------ int main () { #define TOTAL1 20 #define TOTAL2 1000000 for (int i1 = 0; i1 < TOTAL1; i1++) { cout << "\t##### " << (i1 + 1) << " #####" << endl; set_start_time(); for (int i2 = 0; i2 < TOTAL2; i2++) { } set_end_time(); show_user_used_time(); } return 0; } //-------------- C++ Code : END -------------- //-------------- Running : BEGIN ------------- %a.exe ##### 1 ##### User time used : 8709 uclocks User time used : 0 sec ##### 2 ##### User time used : 8706 uclocks User time used : 0 sec ##### 3 ##### User time used : 8706 uclocks User time used : 0 sec ##### 4 ##### User time used : 8706 uclocks User time used : 0 sec ##### 5 ##### User time used : 8706 uclocks User time used : 0 sec ##### 6 ##### User time used : 8706 uclocks User time used : 0 sec ##### 7 ##### User time used : 8706 uclocks User time used : 0 sec ##### 8 ##### User time used : 41603 uclocks User time used : 0.054945 sec ##### 9 ##### User time used : 8706 uclocks User time used : 0 sec ##### 10 ##### User time used : 8706 uclocks User time used : 0 sec ##### 11 ##### User time used : 8706 uclocks User time used : 0 sec ##### 12 ##### User time used : 9955 uclocks User time used : 0.054945 sec ##### 13 ##### User time used : 8707 uclocks User time used : 0 sec ##### 14 ##### User time used : 8706 uclocks User time used : 0 sec ##### 15 ##### User time used : 8706 uclocks User time used : 0 sec ##### 16 ##### User time used : 8706 uclocks User time used : 0 sec ##### 17 ##### User time used : 8706 uclocks User time used : 0 sec ##### 18 ##### User time used : 8706 uclocks User time used : 0 sec ##### 19 ##### User time used : 29672 uclocks User time used : 0.054945 sec ##### 20 ##### User time used : 8706 uclocks User time used : 0 sec //--------------- Running : END -------------- =========================== Alex Vinokur mailto:alexvn AT bigfoot DOT com mailto:alexvn AT dr DOT com http://up.to/alexvn http://go.to/alexv_math ===========================
webmaster | delorie software privacy |
Copyright © 2019 by DJ Delorie | Updated Jul 2019 |