Re: question on timing results

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

 



--- Matthew Saltzman <mjs@xxxxxxxxxxxxxxx> 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.
> 

Hi,

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! 
http://discover.yahoo.com/
#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));

	times(&t1);

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

	times(&t2);

	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;
}

[Index of Archives]     [Current Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]     [Fedora Docs]

  Powered by Linux