C language clock function using the example _c language

Source: Internet
Author: User
Tags microsoft c

clock_t clock (void);
Calculates the processor time used by the calling process
Head file is <time.h>

Return Value
Clock returns the number of clock ticks of elapsed processor time. The returned value
is the product of the amount of has elapsed since the start of a process and
The value of the CLOCKS_PER_SEC constant. If The amount of elapsed 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 was 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.

Copy Code code as follows:

/* CLOCK. C:this example prompts for how long
* The program are 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 = 600000000L;
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 does%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 ())

}//sleep

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.