Документ взят из кэша поисковой машины. Адрес
оригинального документа
: http://mavr.sao.ru/hq/sts/linux/book/c_marshall/node21.html
Дата изменения: Unknown Дата индексирования: Fri Dec 28 19:22:45 2007 Кодировка: Поисковые слова: п п п п п п п п п п п п п п п п п п п п п п п п п р п |
There are many more time functions than we consider here - see man pages and standard library function listings for full details. In this chapter we concentrate on applications of timing functions in C
Uses of time functions include:
Some of thge basic time functions are prototypes as follows:
time_t time(time_t *tloc) -- returns the time since 00:00:00 GMT,
Jan. 1, 1970, measured in seconds.
If tloc is not NULL, the return value is
also stored in the location to which tloc points.
time() returns the value
of time on success.
On failure, it returns (time_t) -1. time_t is
typedefed to a long (int) in <sys/types.h> and
<sys/time.h> header files.
int ftime(struct timeb *tp) -- fills in a
structure pointed to by tp, as defined in
<sys/timeb.h>:
struct timeb
{ time_t time;
unsigned short millitm;
short timezone;
short dstflag;
};
The structure contains the time since the epoch in seconds, up to 1000
milliseconds of more precise interval, the local time zone (measured in
minutes of time westward from Greenwich), and a flag that, if nonzero,
indicates that Day light Saving time applies locally during the appropriate
part of the year.
On success, ftime() returns no useful value. On failure,
it returns -1.
Two other functions defined etc. in #include <time.h>
char *ctime(time_t *clock),
char *asctime(struct tm *tm)
ctime() converts a long integer, pointed to by clock, to a
26-character string of the form produced by asctime(). It first breaks
down clock to a tm structure by calling localtime(), and then
calls asctime() to convert that tm structure to a string.
asctime() converts a time value contained in a tm structure to a
26-character string of the form:
Sun Sep 16 01:03:52 1973
asctime() returns a pointer to the string.
we mentioned above three possible uses of time functions (there are many more) but these are very common.
This is a simple program that illustrates that calling the time function at distinct moments and noting the different times is a simple method of timing fragments of code:
/* timer.c */
#include <stdio.h>
#include <sys/types.h>
#include <time.h>
main()
{ int i;
time_t t1,t2;
(void) time(&t1);
for (i=1;i<=300;++i)
printf(``%d %d %dn'',i, i*i, i*i*i);
(void) time(&t2);
printf(``n Time to do 300 squares and
cubes= %d secondsn'', (int) t2-t1);
}
We have seen a similar example previously, this time we use the lrand48() function to generate of number sequence:
/* random.c */
#include <stdio.h>
#include <sys/types.h>
#include <time.h>
main()
{ int i;
time_t t1;
(void) time(&t1);
srand48((long) t1);
/* use time in seconds to set seed */
printf(``5 random numbers
(Seed = %d):n'',(int) t1);
for (i=0;i<5;++i)
printf(``%d '', lrand48());
printf(``nn''); /* flush print buffer */
}
lrand48() returns non-negative long integers uniformly distributed over the interval (0, 2**31).
A similar function drand48() returns double precision numbers in the range [0.0,1.0).
srand48() sets the seed for these random number generators. It is important to have different seeds when we call the functions otherwise the same set of pseudo-random numbers will generated. time() always provides a unique seed.
Exercise 12708
Write a C program that times a fragment of code in milliseconds.
Exercise 12709
Write a C program to produce a series of floating point random numbers in the ranges (a) 0.0 - 1.0 (b) 0.0 - n where n is any floating point value. The seed should be set so that a unique sequence is guaranteed.