Bonediggerninja
Bonediggerninja

Reputation: 49

Can someone give me an explanation of this function?

I've been wanting a pause function for a while and I found this. Being a beginner in C, I can't be for sure but it looks like functions from <clock.h>. I would like to implement this into my code, but not without understanding it.

void wait(int seconds){
    clock_t start, end;
    start = clock();
    end = clock();
    while (((end-start) / CLOCKS_PER_SEC) = !seconds)
        end = clock();
}

Upvotes: 1

Views: 678

Answers (4)

nikartix
nikartix

Reputation: 717

clock function in <ctime>:

Returns the processor time consumed by the program.

The value returned is expressed in clock ticks, which are units of time of a constant but system-specific length (with a relation of CLOCKS_PER_SEC clock ticks per second).

reference

So, basically it returns number of processor ticks passed since the start of the program. While the processor tick is the number of processor instructions executed by a process, it does not account for IO time and any such that does not use CPU.

CLOCKS_PER_SEC Is the average number of CPU ticks executed by machine and varies from machine to machine, even it (probably) changes time to time, because doing too much IO will cause overall decrease for each process CLOCKS_PER_SEC because more time will be spent not using CPU.

Also this statement: (end-start) / CLOCKS_PER_SEC) = !seconds is not correct, because the right implementation is

while (((end-start) / CLOCKS_PER_SEC) != seconds)
    end = clock();

Does the trick of busy waiting, program will be trapped inside this while loop until seconds seconds will be passed using CPU clocks and CLOCKS_PER_SEC to determine time passed.

Although I would suggest changing it to:

while (((end-start) / CLOCKS_PER_SEC) < seconds)
    end = clock();

Because if process has low priority, or computer is too busy handling many processes chance is one CPU tick can take more than one second (probably when system is crashed, for some buggy program who take up a lot of resources and has high enough priority to cause CPU starvation).

Finally, I do not recommend using it, because you are still using CPU while waiting which can be avoided by using sleep tools discussed here

Upvotes: 1

Shuvo Sarker
Shuvo Sarker

Reputation: 182

The clock() function returns the number of system clock cycles that have occurred since the program began execution. Here is the prototype: clock_t clock(void);

Note that, the return value is not in seconds. To convert the result into seconds, you have to divide the return value by CLOCKS_PER_SEC macro. You program just does this. Functionally it stops the program execution for seconds seconds (you pass this value as an argument to the wait function).

By the way, it uses time.h, not clock.h. And it is not the right point to start learning C.

To learn more: http://www.cplusplus.com/reference/ctime/?kw=time.h

Upvotes: 1

Paul R
Paul R

Reputation: 212969

It's just a busy-wait loop, which is a very nasty way of implementing a delay, because it pegs the CPU at 100% while doing nothing. Use sleep() instead:

#include <unistd.h>

void wait(int seconds)
{
    sleep(seconds);
}

Also note that the code given in the question is buggy:

while (((end-start) / CLOCKS_PER_SEC) = !seconds)

should be:

while (((end-start) / CLOCKS_PER_SEC) != seconds)

or better still:

while (((end-start) / CLOCKS_PER_SEC) < seconds)

(but as mentioned above, you shouldn't even be using this code anyway).

Upvotes: 5

Jaffer Wilson
Jaffer Wilson

Reputation: 7273

Generally, clock_t is a structure in library. It returns the number of clock ticks elapsed from the program initiation till end or till you want to count the time.

If you want to know more you can read details here: http://www.tutorialspoint.com/c_standard_library/c_function_clock.htm

Upvotes: 1

Related Questions