I read that the clock function returns the elapsed processor time (as measured in clock ticks) since the beginning of the program execution. But then another source says "clock() returns 0 the first time it is called (and thereafter it returns the time since it was first called)". So which is it? Does it begin when program starts or when it is called?
The clock function 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.
The way I understand this is that it starts counting when the program starts but that it doesn't necessarily starts counting from zero.
Nothing more than this is guaranteed:
The difference between the two values returned by two different calls to std::clock() gives an estimate of the processor time used by the process between the two calls.
1 2 3 4 5 6 7 8
constauto start = std::clock() ;
// execute some code to be timed
constauto end = std::clock() ;
std::cout << "that code took approximately "
<< double(end-start) / CLOCKS_PER_SEC << " processor seconds.\n" ;
...and even then there is variability. Is it total time elapsed since program start? Or total time consumed by the program's process alone? (The answer depends on the compiler.)