The C 'Clock()' Function Just Returns a Zero

The C `clock()` function just returns a zero

clock() reports CPU time used. sleep() doesn't use any CPU time. So your result is probably exactly correct, just not what you want.

why C clock() returns 0

clock function does not measure CPU clock cycles.

C says clock "returns the implementation’s best approximation to the processor
time used by the program since the beginning of an implementation-defined era related
only to the program invocation."

If between two successive clock calls you program takes less time than one unity of the clock function, you could get 0.

POSIX clock defines the unity with CLOCKS_PER_SEC as 1000000 (unity is then 1 microsecond).

http://pubs.opengroup.org/onlinepubs/009604499/functions/clock.html

To measure clock cycles in x86/x64 you can use inline assembly to retreive the clock count of the CPU Time Stamp Counter register rdtsc.

clock() function always returning 0

clock() returns the number of ticks USED SINCE THE PROGRAM STARTED executing. There is no need (in this specific) example to get the clock_t begin value.

Try printing out both the begin and end values and see what they are. They're likely both 0 or close to 0 as waiting for user input doesn't use CPU time.

Either way, I recommend the time() function as you don't need tick precision.
http://www.cplusplus.com/reference/clibrary/ctime/time/

clock() just returns 0

The clock() function is a coarse measure of CPU time used. Your code doesn't use enough CPU time to measure with such a coarse measure. You should probably switch to something like getrusage instead.

clock() returns 0

The value returned by clock() is of type clock_t (an implementation-defined arithmetic type). It represents "implementation’s best approximation to the processor
time used by the program since the beginning of an implementation-defined era related
only to the program invocation" (N1570 7.27.2.1).

Given a clock_t value, you can determine the number of seconds it represents by multiplying it by CLOCKS_PER_SEC, an implementation-defined macro defined in <time.h>. POSIX requires CLOCKS_PER_SEC to be one million, but it may have different values on different systems.

Note in particular, that the value of CLOCKS_PER_SEC does not necessarily correspond to the actual precision of the clock() function.

Depending on the implementation, two successive calls to clock() might return the same value if the amount of CPU time consumed is less than the precision of the clock() function. On one system I tested, the resolution of the clock() function is 0.01 second; the CPU can execute a lot of instructions in that time.

Here's a test program:

#include <stdio.h>
#include <time.h>
#include <limits.h>
int main(void) {
long count = 0;
clock_t c0 = clock(), c1;
while ((c1 = clock()) == c0) {
count ++;
}
printf("c0 = %ld, c1 = %ld, count = %ld\n",
(long)c0, (long)c1, count);
printf("clock_t is a %d-bit ", (int)sizeof (clock_t) * CHAR_BIT);
if ((clock_t)-1 > (clock_t)0) {
puts("unsigned integer type");
}
else if ((clock_t)1 / 2 == 0) {
puts("signed integer type");
}
else {
puts("floating-point type");
}
printf("CLOCKS_PER_SEC = %ld\n", (long)CLOCKS_PER_SEC);
return 0;
}

On one system (Linux x86_64), the output is:

c0 = 831, c1 = 833, count = 0
clock_t is a 64-bit signed integer type
CLOCKS_PER_SEC = 1000000

Apparently on that system the clock() function's actual resolution is one or two microseconds, and two successive calls to clock() return distinct values.

On another system (Solaris SPARC), the output is:

c0 = 0, c1 = 10000, count = 10447
clock_t is a 32-bit signed integer type
CLOCKS_PER_SEC = 1000000

On that system, the resolution of the clock() function is 0.01 second (10,000 microseconds), and the value returned by clock() did not change for several thousand iterations.

There's (at least) one more thing to watch out for. On a system where clock_t is 32 bits, with CLOCKS_PER_SEC == 1000000, the value can wrap around after about 72 minutes of CPU time, which could be significant for long-running programs. Consult your system's documentation for the details.

What should main() return in C and C++?

The return value for main indicates how the program exited. Normal exit is represented by a 0 return value from main. Abnormal exit is signaled by a non-zero return, but there is no standard for how non-zero codes are interpreted. As noted by others, void main() is prohibited by the C++ standard and should not be used. The valid C++ main signatures are:

int main()

and

int main(int argc, char* argv[])

which is equivalent to

int main(int argc, char** argv)

It is also worth noting that in C++, int main() can be left without a return-statement, at which point it defaults to returning 0. This is also true with a C99 program. Whether return 0; should be omitted or not is open to debate. The range of valid C program main signatures is much greater.

Efficiency is not an issue with the main function. It can only be entered and left once (marking the program's start and termination) according to the C++ standard. For C, re-entering main() is allowed, but should be avoided.



Related Topics



Leave a reply



Submit