How do I set the timer in C?

Updated on technology 2024-04-01
2 answers
  1. Anonymous users2024-02-07

    #include

    clock_t start,end;

    int t;

    Write at the beginning of the chronograph:

    start = clock();

    At the end of the chronograph write:

    end = clock();

    t=(end - start)/clocks_per_sec;

    printf("you use % seconds", t);

  2. Anonymous users2024-02-06

    clock() function:

    clock() is a timing function in C C++, and the data type associated with it is clock t. In MSDN, the clock function is defined as follows:

    clock_t

    clock(void)

    This function returns the CPU clock timing unit (clock) between the time "start this program process" and the time "the clock() function is called in the program".

    tick), which is called a wall-clock in MSDN; If the wall clock time is not desirable, -1 is returned. where clock t is the data type used to store time, and in the file we can find the definition of it:

    #ifndef

    clock_t_defined

    typedef

    longclock_t;

    #define

    clock_t_defined

    #endif

    Obviously, clock t is a long integer number. In the file, a constant clocks per sec is also defined, which is used to indicate how many clock timing units there will be in a second, which is defined as follows:

    #define

    clocks_per_sec

    clock_t)1000)

    You can see that every thousandth of a second (1 millisecond), the value returned by calling the clock() function is increased by 1. As an example, you can use the formula clock() clocks per sec to calculate the runtime of a process itself

    voidelapsed_time()

    #include

    #include

    #include

    intmain(void)

    On the author's machine, the result is as follows:

    timetodo

    emptyloopsis

    seconds

    Above we saw that the length of the clock timing unit is 1 millisecond, so the accuracy of the timing is also 1 millisecond, so can we change the definition of clocks per sec by defining it larger, so that the timing accuracy is higher? Try it and you'll find that it doesn't work. In standard C++, the smallest unit of timekeeping is one millisecond.

    time_t

    time(time_t

    The timer return value is the number of seconds from 1970 to the present.

    It will be possible to connect it with a long type.

    The same is true for parameters.

    Such as longtime s

    time_s

    time(null

    Time s is the number of seconds from 1970 to the present.

    Or long

    time_s

    null;time(time_s);

    Time s is the number of seconds from 1970 to the present.

    If you want to calculate the period of time before and after, take the time once before, and subtract it once after that to know how many seconds it takes.

Related questions
15 answers2024-04-01

Indicates that the remainder of 6 is taken.

In the C language, % stands for remainder (or modulo operation). >>>More

12 answers2024-04-01

This is an additive function I wrote that can judge overflow: >>>More

11 answers2024-04-01

srand(int) is used to set the seed, and then returns a random value each time rand(). >>>More

11 answers2024-04-01

Valid variable names for the C language:

First, it can only contain numbers, letters, and underscores. >>>More

8 answers2024-04-01

The semicolon is the sign of the end of the statement, but the semicolon is not used after the loop, if, and subfunctions such as long long a (int b), and everything else is used, but two semicolons cannot be added, although it will not cause an error, but it may affect the result.