[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: question on timing results

--- Matthew Saltzman <mjs ces clemson edu> wrote:

> On Thu, 9 Jun 2005, Globe Trotter wrote:
> >
> >>> clock() returns an approximation of the CPU time used by the actual
> >> process,
> >>> so while load from other programs is a factor (particularly since it'll
> >> make
> >>> your program wait for IO, etc.) it should do basically what you want.
> >>
> >> Also check out the times(2) facility.  It's not ISO C, but it is POSIX and
> >> it has some advantages over clock(3).
> >>
> >> --
> >>
> >
> > Thanks, what exactly are the advantages? These are sometimes long 
> > calculations so the processor will be running for long. Also, is there
> clock() will wrap after about 75 CPU minutes.  I believe times() will not.
> Also times() tracks user and system time separately, and it tracks parent 
> and child process times separately.
> > an example on using times(2) somewhere? A Google search on times(2) 
> > example C did not reveal much, but the choice of keywords may not have 
> > been apt. (One problem with the name of the language called C is that is 
> > just a letter, as opposed to fortran/C++/pascal et al, which is more 
> > distinctive.
> Don't know of a handy URL, sorry.  The man page ("man 2 times") and a 
> little experimentation should be enough.


Here is an example cooked up just to show how it works. Only done with utime
and stime, but can be extended to the other two in exactly the same way. But I
wonder, which should be used? As per the manpage for times(2), the function
reports the value of four times in four fields. 1)  The tms_utime field
contains the CPU time spent executing  instructions of  the  calling  process. 
2) The  tms_stime field contains the CPU time spent in the system while
executing tasks on behalf of the calling process.  3) The  tms_cutime  field 
contains  the  sum of the tms_utime and  tms_cutime  values  for  all 
waited-for  terminated   children. 4) The tms_cstime  field contains the sum of
the tms_stime and tms_cstime values for all waited-for terminated children.

My programs go into an algorithm and do lots of things. Which times shoudl I
use?  I don't understand all this much: I am considering using the tms_utime
field, but I don't know if I should use that or the sum of the first two or all
of them. Any suggestions: I guess I really don't know what all these 
different times mean.

Many thanks and best wishes!

Discover Yahoo! 
Use Yahoo! to plan a weekend, have fun online and more. Check it out! 
#include <stdio.h>
#include <unistd.h>	/* for sysconf() */
#include <limits.h>	/* for LONG_MAX */
#include <assert.h>	/* for assert() */
#include <sys/times.h>

int main(void)
	struct tms t1, t2;
	long ticks_per_sec;
	clock_t utime, stime;
	double sum=0.0;
	int i;

	ticks_per_sec = sysconf(_SC_CLK_TCK);
	printf("clock ticks = %ld per second\n", ticks_per_sec);
	printf("size of clock_t = %u\n", sizeof(clock_t));


	for (i=1; i<=1000000000; i++)
		sum += 1.0/i;


	printf("sum = %g\n", sum);

	utime = t2.tms_utime - t1.tms_utime;
	stime = t2.tms_stime - t1.tms_stime;

	/* adjust in case clock has turned over, assuming clock_t
	   equals long int
	assert(sizeof(clock_t) == sizeof(long));

	if (utime < 0)
		utime += LONG_MAX;

	if (stime < 0)
		stime += LONG_MAX;

	printf("utime = %ld ticks = %g seconds\n",
			utime, (double)utime/ticks_per_sec);

	printf("stime = %ld ticks = %g seconds\n",
			stime, (double)stime/ticks_per_sec);

	return 0;

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]