Measuring elapsed time

What is the best and/or simplest way to get and measure the difference between two times?

I saw this but wanted to know if there was a better or different way to get similar results
1
2
3
4
5
6
7
8
9
10
11
12
  #include <time.h>
time_t start,end;
time (&start);
.
.
.
<your code>
.
.
.
time (&end);
double dif = difftime (end,start);


Thanks.
closed account (2AoiNwbp)
Hi,

time() returns time in seconds, while clock() returns time in milliseconds. Here you have an example. For instance, it took 4 millis in my computer to randomize the whole array. (perhaps it took less, but I don't know of a more precise function - not in ctime)

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
#include <iostream>
#include <time.h>

using std::cout;
using std::endl;

int main()
{
	clock_t t;
	t = clock();

	size_t size = 100000;
	int *pInt = new int[size];
	for(size_t i = 0; i < size; i++)
		pInt[i] = rand();

	t = clock() - t;
	cout << "time: " << t << " miliseconds" << endl;
	cout << CLOCKS_PER_SEC << " clocks per second" << endl;
	cout << "time: " << t*1.0/CLOCKS_PER_SEC << " seconds" << endl;

	system("pause");
	delete [] pInt;
	return 0;
}


regards,
Alejandro
Topic archived. No new replies allowed.