time elapsed.

Hey Guys!.
I'm not able to find the reason i got 0 each time. Can you pls help me?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <ctime>
using namespace std;

class timer {
public:
	timer();
	~timer();
private:
	clock_t start;
	clock_t end;
	clock_t timeElapsed;
};

timer::timer()
{
	start = clock();
}

timer::~timer()
{
	end = clock();
	timeElapsed = (end - start) / (CLOCKS_PER_SEC/1000);
	cout << timeElapsed << endl;
}

int main(int argc, char *argv[])
{
	timer ob;
	char c;

	cout << "Press any key to continue..: " << endl;
	cin >> c;

	return 0;
}
There may be other reasons, but use of double may help:

1
2
3
4
5
6
timer::~timer()
{
	end = clock();
	double timeElapsed = static_cast<double>(end - start) / CLOCKS_PER_SEC;
	cout << timeElapsed << endl;
}
Last edited on
there are several topics on this site about better quality time functions. Clock works fine but it fails for things that complete very quickly, so be sure that whatever you use it for is at least a ms or two duration.

what are those "better quality time functions" ??
clock() measures processor time, so even after Chervil's fix you'll get 0.

https://en.wikipedia.org/wiki/CPU_time
process time is the amount of time for which a CPU was used for processing instructions of a computer program, as opposed to, for example, waiting for input/output (I/O) operations
It seems it's os specific? On Windows clock() seems to return wall clock time.
https://stackoverflow.com/questions/17432502/how-can-i-measure-cpu-time-and-wall-clock-time-on-both-linux-windows
How about

http://www.cplusplus.com/reference/chrono/high_resolution_clock/now/

I was using rdtsc register on intel before they invented variable speed CPU clock, which ruined that for wall clock but it still is accurate on how many operations a task took. I think you have to use emit + hex code to invoke it with inline assembler. Before the new cpus you could divide by cpu hz to get wall time, and its still close on that for small periods but its not exact anymore.
Last edited on
> On Windows clock() seems to return wall clock time.
¿no pueden hacer nada bien?

https://msdn.microsoft.com/en-us/library/4e2ess30.aspx
Note that this function does not strictly conform to ISO C, which specifies net CPU time as the return value.
«strictly»
jonnin wrote:
I was using rdtsc register on intel before they invented variable speed CPU clock, which ruined that for wall clock but it still is accurate on how many operations a task took

You can go back to using rdtsc for wall clock time.
Quoting 2011 stackoverflow post https://stackoverflow.com/a/4145156 quoting intel,

The invariant TSC will run at a constant rate in all ACPI P-, C-. and T-states. This is the architectural behavior moving forward. On processors with invariant TSC support, the OS may use the TSC for wall clock timer services (instead of ACPI or HPET timers). TSC reads are much more efficient and do not incur the overhead associated with a ring transition or access to a platform resource."
That is awesome news. I havent been doing real time coding, I actually on a database that can somehow spend 10+ min doing what C can do in 5 nanoseconds, but that is a great tool for anyone doing performance tuning or precisely timed code.

Topic archived. No new replies allowed.