is there a way to measure time elapsed using a c++ program?

I went through this forum and got to know that the time() does this. But, I'm not quite sure I understand how to use this function.
Here is an outline of the code that I have so far
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <ctime>
using namespace std;

int main(){
     clock_t start = clock();
//code here
     clock_t ends = clock();
     cout << "Running Time : " << (double) (ends - start) / CLOCKS_PER_SEC << endl;

     return 0;
}
#

This program always tends to return clock seconds as 0. I'm not sure what the issue is
There is no issue - code runs really fast, and most likely it is giving you zero because it runs in less than a second.
Thanks a lot .I wanted to measure the time for various sorting algorithms. I'm going to run this for each one and post my observations.
hey look at this just do some adjustments..


/* difftime example */
#include <stdio.h>
#include <ctime>
#include <stdlib.h>


int main ()
{


time_t start,end;
char szInput [256];
double dif;

time (&start);
printf ("Please, enter your name: ");
gets (szInput);
time (&end);
dif = difftime (end,start);
printf ("Hi %s.\n", szInput);
printf ("It took you %.2lf seconds to type your name.\n", dif );

return 0;
}

//printf does the same as cout here and just adject the code to look like more of the c++ you know..not my code
Last edited on
Topic archived. No new replies allowed.