Mail Archives: cygwin/2003/10/17/03:58:21
"Alex Vinokur" <alexvn AT connect DOT to> wrote in message news:bmhbv9$fu1$2 AT sea DOT gmane DOT org...
> clock : "Calculates the best available approximation of the cumulative amount of
> time used by your program since it started." (From man clock).
>
> Does clock() calculate user-used-time + system-used-time?
>
Yes, it is.
===========================================
Windows 2000 Professional
CYGWIN_NT-5.0 1.5.4(0.94/3/2)
gcc version 3.3.1 (cygming special)
===========================================
========= C code : File t.c : BEGIN =========
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/resource.h>
#include <assert.h>
int main(int argc, char** argv)
{
clock_t start_clock,
end_clock;
struct rusage start_rusage,
end_rusage;
clock_t diff_clock;
double diff_rusage_user,
diff_rusage_system;
int i;
assert (argc > 1);
fprintf (stderr, "--------- Start ---------\n");
start_clock = clock();
getrusage(RUSAGE_SELF, &start_rusage);
for (i = 0; i < atoi (argv[1]); i++) printf ("ABC\n");
getrusage(RUSAGE_SELF, &end_rusage);
end_clock = clock();
assert (!(start_clock == (clock_t)(-1)));
assert (!(end_clock == (clock_t)(-1)));
diff_clock = end_clock - start_clock;
fprintf (stderr, "\n");
fprintf (stderr, "clock : start = %lu ticks, end = %lu ticks\n", start_clock, end_clock);
fprintf (stderr, "clock : elapsed time = %.2f sec\n", (double)(diff_clock)/CLOCKS_PER_SEC);
diff_rusage_user =
(end_rusage.ru_utime.tv_sec - start_rusage.ru_utime.tv_sec)
+
(end_rusage.ru_utime.tv_usec - start_rusage.ru_utime.tv_usec)/ 1.0e6;
fprintf (stderr, "\n");
fprintf (stderr, "rusage user : start = (%ld sec, %ld msec), end = (%ld sec, %ld msec)\n",
start_rusage.ru_utime.tv_sec,
start_rusage.ru_utime.tv_usec,
end_rusage.ru_utime.tv_sec,
end_rusage.ru_utime.tv_usec
);
fprintf (stderr, "rusage user : elapsed time = %.2f sec\n", diff_rusage_user);
diff_rusage_system =
(end_rusage.ru_stime.tv_sec - start_rusage.ru_stime.tv_sec)
+
(end_rusage.ru_stime.tv_usec - start_rusage.ru_stime.tv_usec)/ 1.0e6;
fprintf (stderr, "\n");
fprintf (stderr, "rusage system : start = (%ld sec, %ld msec), end = (%ld sec, %ld msec)\n",
start_rusage.ru_stime.tv_sec,
start_rusage.ru_stime.tv_usec,
end_rusage.ru_stime.tv_sec,
end_rusage.ru_stime.tv_usec
);
fprintf (stderr, "rusage system : elapsed time = %.2f sec\n", diff_rusage_system);
fprintf (stderr, "\n--------- Finish --------\n\n");
return 0;
}
========= C code : File t.c : END ===========
========= Compilation & Run : BEGIN ===========
$ gcc t.c
$ a 10000
--------- Start ---------
ABC
ABC
ABC
[---omitted---]
ABC
ABC
ABC
clock : start = 10 ticks, end = 900 ticks
clock : elapsed time = 0.89 sec
rusage user : start = (0 sec, 10000 msec), end = (0 sec, 540000 msec)
rusage user : elapsed time = 0.53 sec
rusage system : start = (0 sec, 0 msec), end = (0 sec, 360000 msec)
rusage system : elapsed time = 0.36 sec
--------- Finish --------
========= Compilation & Run : END =============
We can see that
clock elapsed time (0.89) == rusage user elapsed time (0.53) + rusage system elapsed time (0.36)
=====================================
Alex Vinokur
mailto:alexvn AT connect DOT to
http://mathforum.org/library/view/10978.html
news://news.gmane.org/gmane.comp.lang.c++.perfometer
=====================================
--
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
Problem reports: http://cygwin.com/problems.html
Documentation: http://cygwin.com/docs.html
FAQ: http://cygwin.com/faq/
- Raw text -