time & clock(MSDN)
2007-05-13 15:20
176 查看
clock
Calculates the processor time used by the calling process.
clock_t clock( void );
Routine Required Header Compatibility
clock <time.h> ANSI, Win 95, Win NT
For additional compatibility information, see Compatibility in the Introduction.
Libraries
LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version
Return Value
clock returns the number of clock ticks of elapsed processor time. The returned value is the product of the amount of time that has elapsed since the start of a process and the value of the CLOCKS_PER_SEC constant. If the amount of elapsed time is unavailable, the function returns –1, cast as a clock_t.
Remarks
The clock function tells how much processor time the calling process has used. The time in seconds is approximated by dividing the clock return value by the value of the CLOCKS_PER_SEC constant. In other words, clock returns the number of processor timer ticks that have elapsed. A timer tick is approximately equal to 1/CLOCKS_PER_SEC second. In versions of Microsoft C before 6.0, the CLOCKS_PER_SEC constant was called CLK_TCK.
Example
/* CLOCK.C: This example prompts for how long
* the program is to run and then continuously
* displays the elapsed time for that period.
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void sleep( clock_t wait );
void main( void )
{
long i = 600000L;
clock_t start, finish;
double duration;
/* Delay for a specified time. */
printf( "Delay for three seconds/n" );
sleep( (clock_t)3 * CLOCKS_PER_SEC );
printf( "Done!/n" );
/* Measure the duration of an event. */
printf( "Time to do %ld empty loops is ", i );
start = clock();
while( i-- )
;
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf( "%2.1f seconds/n", duration );
}
/* Pauses for a specified number of milliseconds. */
void sleep( clock_t wait )
{
clock_t goal;
goal = wait + clock();
while( goal > clock() )
;
}
Output
Delay for three seconds
Done!
Time to do 600000 empty loops is 0.1 seconds
Time Management Routines
See Also difftime, time
difftime
Finds the difference between two times.
double difftime( time_t timer1, time_t timer0 );
Routine Required Header Compatibility
difftime <time.h> ANSI, Win 95, Win NT
For additional compatibility information, see Compatibility in the Introduction.
Libraries
LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version
Return Value
difftime returns the elapsed time in seconds, from timer0 to timer1. The value returned is a double-precision floating-point number.
Parameters
timer1
Ending time
timer0
Beginning time
Remarks
The difftime function computes the difference between the two supplied time values timer0 and timer1.
Example
/* DIFFTIME.C: This program calculates the amount of time
* needed to do a floating-point multiply 10 million times.
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void main( void )
{
time_t start, finish;
long loop;
double result, elapsed_time;
printf( "Multiplying 2 floating point numbers 10 million times.../n" );
time( &start );
for( loop = 0; loop < 10000000; loop++ )
result = 3.63 * 5.27;
time( &finish );
elapsed_time = difftime( finish, start );
printf( "/nProgram takes %6.0f seconds./n", elapsed_time );
}
Output
Multiplying 2 floats 10 million times...
Program takes 2 seconds.
time
Gets the system time.
time_t time( time_t *timer );
Routine Required Header Compatibility
time <time.h> ANSI, Win 95, Win NT
For additional compatibility information, see Compatibility in the Introduction.
Libraries
LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version
Return Value
time returns the time in elapsed seconds. There is no error return.
Parameter
timer
Storage location for time
Remarks
The time function returns the number of seconds elapsed since midnight (00:00:00), January 1, 1970, coordinated universal time, according to the system clock. The return value is stored in the location given by timer. This parameter may be NULL, in which case the return value is not stored.
Example
/* TIMES.C illustrates various time and date functions including:
* time _ftime ctime asctime
* localtime gmtime mktime _tzset
* _strtime _strdate strftime
*
* Also the global variable:
* _tzname
*/
#include <time.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/timeb.h>
#include <string.h>
void main()
{
char tmpbuf[128], ampm[] = "AM";
time_t ltime;
struct _timeb tstruct;
struct tm *today, *gmt, xmas = { 0, 0, 12, 25, 11, 93 };
/* Set time zone from TZ environment variable. If TZ is not set,
* the operating system is queried to obtain the default value
* for the variable.
*/
_tzset();
/* Display operating system-style date and time. */
_strtime( tmpbuf );
printf( "OS time:/t/t/t/t%s/n", tmpbuf );
_strdate( tmpbuf );
printf( "OS date:/t/t/t/t%s/n", tmpbuf );
/* Get UNIX-style time and display as number and string. */
time( <ime );
printf( "Time in seconds since UTC 1/1/70:/t%ld/n", ltime );
printf( "UNIX time and date:/t/t/t%s", ctime( <ime ) );
/* Display UTC. */
gmt = gmtime( <ime );
printf( "Coordinated universal time:/t/t%s", asctime( gmt ) );
/* Convert to time structure and adjust for PM if necessary. */
today = localtime( <ime );
if( today->tm_hour > 12 )
{
strcpy( ampm, "PM" );
today->tm_hour -= 12;
}
if( today->tm_hour == 0 ) /* Adjust if midnight hour. */
today->tm_hour = 12;
/* Note how pointer addition is used to skip the first 11
* characters and printf is used to trim off terminating
* characters.
*/
printf( "12-hour time:/t/t/t/t%.8s %s/n",
asctime( today ) + 11, ampm );
/* Print additional time information. */
_ftime( &tstruct );
printf( "Plus milliseconds:/t/t/t%u/n", tstruct.millitm );
printf( "Zone difference in seconds from UTC:/t%u/n",
tstruct.timezone );
printf( "Time zone name:/t/t/t/t%s/n", _tzname[0] );
printf( "Daylight savings:/t/t/t%s/n",
tstruct.dstflag ? "YES" : "NO" );
/* Make time for noon on Christmas, 1993. */
if( mktime( &xmas ) != (time_t)-1 )
printf( "Christmas/t/t/t/t%s/n", asctime( &xmas ) );
/* Use time structure to build a customized time string. */
today = localtime( <ime );
/* Use strftime to build a customized time string. */
strftime( tmpbuf, 128,
"Today is %A, day %d of %B in the year %Y./n", today );
printf( tmpbuf );
}
Output
OS time: 21:51:03
OS date: 05/03/94
Time in seconds since UTC 1/1/70: 768027063
UNIX time and date: Tue May 03 21:51:03 1994
Coordinated universal time: Wed May 04 04:51:03 1994
12-hour time: 09:51:03 PM
Plus milliseconds: 279
Zone difference in seconds from UTC: 480
Time zone name:
Daylight savings: YES
Christmas Sat Dec 25 12:00:00 1993
Today is Tuesday, day 03 of May in the year 1994.
Time Management Routines
See Also asctime, _ftime, gmtime, localtime, _utime
Calculates the processor time used by the calling process.
clock_t clock( void );
Routine Required Header Compatibility
clock <time.h> ANSI, Win 95, Win NT
For additional compatibility information, see Compatibility in the Introduction.
Libraries
LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version
Return Value
clock returns the number of clock ticks of elapsed processor time. The returned value is the product of the amount of time that has elapsed since the start of a process and the value of the CLOCKS_PER_SEC constant. If the amount of elapsed time is unavailable, the function returns –1, cast as a clock_t.
Remarks
The clock function tells how much processor time the calling process has used. The time in seconds is approximated by dividing the clock return value by the value of the CLOCKS_PER_SEC constant. In other words, clock returns the number of processor timer ticks that have elapsed. A timer tick is approximately equal to 1/CLOCKS_PER_SEC second. In versions of Microsoft C before 6.0, the CLOCKS_PER_SEC constant was called CLK_TCK.
Example
/* CLOCK.C: This example prompts for how long
* the program is to run and then continuously
* displays the elapsed time for that period.
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void sleep( clock_t wait );
void main( void )
{
long i = 600000L;
clock_t start, finish;
double duration;
/* Delay for a specified time. */
printf( "Delay for three seconds/n" );
sleep( (clock_t)3 * CLOCKS_PER_SEC );
printf( "Done!/n" );
/* Measure the duration of an event. */
printf( "Time to do %ld empty loops is ", i );
start = clock();
while( i-- )
;
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf( "%2.1f seconds/n", duration );
}
/* Pauses for a specified number of milliseconds. */
void sleep( clock_t wait )
{
clock_t goal;
goal = wait + clock();
while( goal > clock() )
;
}
Output
Delay for three seconds
Done!
Time to do 600000 empty loops is 0.1 seconds
Time Management Routines
See Also difftime, time
difftime
Finds the difference between two times.
double difftime( time_t timer1, time_t timer0 );
Routine Required Header Compatibility
difftime <time.h> ANSI, Win 95, Win NT
For additional compatibility information, see Compatibility in the Introduction.
Libraries
LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version
Return Value
difftime returns the elapsed time in seconds, from timer0 to timer1. The value returned is a double-precision floating-point number.
Parameters
timer1
Ending time
timer0
Beginning time
Remarks
The difftime function computes the difference between the two supplied time values timer0 and timer1.
Example
/* DIFFTIME.C: This program calculates the amount of time
* needed to do a floating-point multiply 10 million times.
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void main( void )
{
time_t start, finish;
long loop;
double result, elapsed_time;
printf( "Multiplying 2 floating point numbers 10 million times.../n" );
time( &start );
for( loop = 0; loop < 10000000; loop++ )
result = 3.63 * 5.27;
time( &finish );
elapsed_time = difftime( finish, start );
printf( "/nProgram takes %6.0f seconds./n", elapsed_time );
}
Output
Multiplying 2 floats 10 million times...
Program takes 2 seconds.
time
Gets the system time.
time_t time( time_t *timer );
Routine Required Header Compatibility
time <time.h> ANSI, Win 95, Win NT
For additional compatibility information, see Compatibility in the Introduction.
Libraries
LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version
Return Value
time returns the time in elapsed seconds. There is no error return.
Parameter
timer
Storage location for time
Remarks
The time function returns the number of seconds elapsed since midnight (00:00:00), January 1, 1970, coordinated universal time, according to the system clock. The return value is stored in the location given by timer. This parameter may be NULL, in which case the return value is not stored.
Example
/* TIMES.C illustrates various time and date functions including:
* time _ftime ctime asctime
* localtime gmtime mktime _tzset
* _strtime _strdate strftime
*
* Also the global variable:
* _tzname
*/
#include <time.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/timeb.h>
#include <string.h>
void main()
{
char tmpbuf[128], ampm[] = "AM";
time_t ltime;
struct _timeb tstruct;
struct tm *today, *gmt, xmas = { 0, 0, 12, 25, 11, 93 };
/* Set time zone from TZ environment variable. If TZ is not set,
* the operating system is queried to obtain the default value
* for the variable.
*/
_tzset();
/* Display operating system-style date and time. */
_strtime( tmpbuf );
printf( "OS time:/t/t/t/t%s/n", tmpbuf );
_strdate( tmpbuf );
printf( "OS date:/t/t/t/t%s/n", tmpbuf );
/* Get UNIX-style time and display as number and string. */
time( <ime );
printf( "Time in seconds since UTC 1/1/70:/t%ld/n", ltime );
printf( "UNIX time and date:/t/t/t%s", ctime( <ime ) );
/* Display UTC. */
gmt = gmtime( <ime );
printf( "Coordinated universal time:/t/t%s", asctime( gmt ) );
/* Convert to time structure and adjust for PM if necessary. */
today = localtime( <ime );
if( today->tm_hour > 12 )
{
strcpy( ampm, "PM" );
today->tm_hour -= 12;
}
if( today->tm_hour == 0 ) /* Adjust if midnight hour. */
today->tm_hour = 12;
/* Note how pointer addition is used to skip the first 11
* characters and printf is used to trim off terminating
* characters.
*/
printf( "12-hour time:/t/t/t/t%.8s %s/n",
asctime( today ) + 11, ampm );
/* Print additional time information. */
_ftime( &tstruct );
printf( "Plus milliseconds:/t/t/t%u/n", tstruct.millitm );
printf( "Zone difference in seconds from UTC:/t%u/n",
tstruct.timezone );
printf( "Time zone name:/t/t/t/t%s/n", _tzname[0] );
printf( "Daylight savings:/t/t/t%s/n",
tstruct.dstflag ? "YES" : "NO" );
/* Make time for noon on Christmas, 1993. */
if( mktime( &xmas ) != (time_t)-1 )
printf( "Christmas/t/t/t/t%s/n", asctime( &xmas ) );
/* Use time structure to build a customized time string. */
today = localtime( <ime );
/* Use strftime to build a customized time string. */
strftime( tmpbuf, 128,
"Today is %A, day %d of %B in the year %Y./n", today );
printf( tmpbuf );
}
Output
OS time: 21:51:03
OS date: 05/03/94
Time in seconds since UTC 1/1/70: 768027063
UNIX time and date: Tue May 03 21:51:03 1994
Coordinated universal time: Wed May 04 04:51:03 1994
12-hour time: 09:51:03 PM
Plus milliseconds: 279
Zone difference in seconds from UTC: 480
Time zone name:
Daylight savings: YES
Christmas Sat Dec 25 12:00:00 1993
Today is Tuesday, day 03 of May in the year 1994.
Time Management Routines
See Also asctime, _ftime, gmtime, localtime, _utime
相关文章推荐
- time & clock(MSDN)
- A07_TimePicker & DatePicker & AnalogClock & DigitalClock 的设置小结
- glibc && clock_gettime.c
- undefined reference to `clock_gettime' 链接错误问题解决(转)
- 编译错误:undefined reference to `clock_gettime'
- 使用opencv 2.2静态库时: undefined reference to `clock_gettime' 链接错误问题解决
- undefined reference to `clock_gettime'
- undefined reference to `clock_gettime'
- undefined reference to `clock_gettime' 链接错误问题解决
- Linux Clock & Time
- libQtCore.so: undefined reference to `clock_gettime'
- 错误的程序之阶乘之和测试程序的快慢程度(加一个计时器)<time.h>和clock()函数
- (.text+0xb3a): undefined reference to `clock_gettime'
- undefined reference to `clock_gettime' 链接错误问题解决
- undefined reference to `clock_gettime' 链接错误问题解决
- FC14下错误 /usr/bin/ld: libvalhalla-test.o: undefined reference to symbol 'clock_gettime@@GLIBC_2.2'
- undefined reference to `clock_gettime' 链接错误的解决方案
- Linux Clock & Time
- 【算法分析与设计】【第一周】121.&122. Best Time to Buy and Sell Stock I&II
- 基础·MSDN·不指定边界遍历数组