Timing Efficiency

I have been wondering about this for some time, but is there anyway in C++ to time the length (in milli seconds, seconds etc) of your program. Working on some of the algorithms found in Project Euler, it wold be interesting I think to see if it's possible to make an algorithm quantifiably more efficient.

Note: I only know console.

Thanks,

Mike
by using clock() you can store the time when you start in x. and if you're done you store clock() in another variable like y. with data type clock_t. but actually ill better show you this example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>
#include <iostream.h>
#include <time.h>

double diffclock(clock_t clock1,clock_t clock2)
{
	double diffticks=clock1-clock2;
	double diffms=(diffticks*1000)/CLOCKS_PER_SEC;
       //CLOCKS_PER_SEC represents the number of clock ticks in a second
	return diffms;
} 

int main (void)
{
        char c[1000]; 
	int i;
	clock_t begin=clock();
	cout << "Hi what is your name? ";
	cin >>c;
	clock_t end=clock();
	cout << "Time elapsed: " << double(diffclock(end,begin)) << " ms"<< endl;
	return 0;
}
Last edited on
Fantastic!

Thanks a million!

Mike
Thanks gelatine !!!
no problemo glad to help :p
Topic archived. No new replies allowed.