Home > Back-end >  Clock () function has been a maximum
Clock () function has been a maximum

Time:05-04

Why program to perform clock () function gives the maximum

CodePudding user response:

Clock
Calculates the processor time 2 by the calling process.

Clock_t clock (void);

The Routine of Required Header Compatibility
Clock & lt; Time. H> ANSI, Windows 95, Windows NT


For additional compatibility information, see compatibility in the 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


The 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.

Few

The clock function tells how much processor time The calling process has 2. The time in seconds is approximated by dividing The clock return value by The value of The CLOCKS_PER_SEC constant. In other words, it 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 & lt; Stdio. H>
# include & lt; Stdlib. H>
# include & lt; Time. H>

Void sleep (clock_t wait);

Void main (void)
{
Long I=600000 l;
Clock_t start and finish;
Double duration;

/* Delay for a specified time. */
Printf (" Delay for three seconds \ n ");
Sleep (3 * (clock_t) CLOCKS_PER_SEC);
Printf (" Done! \ n ");

/* Measure the duration of an event. */
Printf (" Time "todo" % ld empty loops is ", I);
Start=clock ();
While (I -)
;
Finish=clock ();
Duration=(double) (finish - start)/CLOCKS_PER_SEC;
Printf (" % f 2.1 seconds \ n ", duration).
}

/* Pauses for a specified number of milliseconds. */
Void sleep (clock_t wait)
{
Clock_t goal;
Goal=wait + clock ();
While (goal & gt; Clock ())
;
}


The Output

Delay for three seconds
Done!
The Time to do 600000 empty loops is 0.1 seconds



Time Management Routines

See Also difftime, time

CodePudding user response:

I looked at the clock function, but I'll direct call the clock directly in the main function, and then the execution, according to the parameters of the corresponding clock_t is 0 XFFFFFF, below is the corresponding procedures;
Int main (void)
{
Clock_t start and end;
Start=clock ();
Sleep (500);
End=clcok ();
Duration=(double) (end - start)/CLOCKS_PER_SEC;
Printf (" % d ", duration);
}
The results showed that the is 0; Then perform step by step, view the corresponding parameters, the start and end are the maximum 0 XFFFFFF,

CodePudding user response:

https://fanyi.baidu.com/#en/zh/Return%20Value%0A%0Aclock%20returns%20the%20number%20of%20clock%20ticks%20of%20elapsed%20processor%20time.%20The%20returned%20value%20is%20the%20product%20of%20the%20amount%20of%20time%20that%20has%20elapsed%20since%20the%20start%20of%20a%20process%20and%20the%20value%20of%20the%20CLOCKS_PER_SEC%20constant.%20If%20the%20amount%20of%20elapsed%20time%20is%20unavailable%2C%20the%20function%20returns%20%E2%80%931%2C%20cast%20as%20a%20clock_t.

CodePudding user response:

//printf (" % d ", duration). 
Printf (" % lf \ n ", duration).

Duration is defined as double type, then % lf output,
The results of the original poster is 0, because do not understand and double type int the storage of data in the memory is not the same, suggested know the building Lord ~

CodePudding user response:

Complete C and C + + tutorial: https://blog.csdn.net/it_xiangqiang/category_10581430.html
C and C + + algorithm complete tutorial: https://blog.csdn.net/it_xiangqiang/category_10768339.html
  • Related