I'm diving into multithreading (or, at least, trying to) and wrote a function that would figure out how long some process took (see double diffClocks(...)):
Using typecast. clock_t is an integer. CLOCKS_PER_SEC is also an integer.
When you divide a(5000) by b(10000), one of two things may happen :
If they are floating numbers : 5000.0 / 10000.0 = 0.5
If they are integers : 5000 / 10000 = 0 (only quotient part is counted)
For measuring the time (as in, the number of seconds) that a process takes, don't use clock – it provides the CPU time, or the number of CPU cycles. Dividing by CLOCKS_PER_SEC won't give entirely accurate results.
Prefer instead some of the functions from <chrono>:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
#include <chrono>
// ...
int main() {
usingnamespace std::chrono;
auto start = steady_clock::now();
// do work here
auto end = steady_clock::now();
double secs = duration_cast<duration<double>>(end - start).count();
std::cout << "Time taken: " << secs << "\n";
return 0;
}