computational time calculation

How do I compute computational time for my main program? I know there is a ctime object but dont know what is the best way to use it. My Main file calls 2 three functions also.
Here's some code that I made in 5 minutes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <ctime>
#include <cstdio>

void myfunc(){
	//do stuff
	unsigned int x=1;
	while(x<4294967295){//something that takes a long time to execute (around 4 seconds on my laptop)
		++x;}}

int main(){
	clock_t ticks=clock();//keeps track of ticks
	myfunc();//your function
	//update the ticks
	ticks-=clock();
	ticks=-ticks;
	//display the time
	printf("myfunc took %i ticks to run.\nPress <ENTER> to exit\n",ticks);
	getchar();
	return 0;}


First you record the number of ticks before using the function, then you record the number of ticks afterwards.
Last edited on
Is the unit of the ticks in seconds?
It is in clock ticks. Use the macro CLOCKS_PER_SEC to convert to seconds.
Topic archived. No new replies allowed.