- #1
ACLerok
- 194
- 0
I am writing a program and I need to measure the time it takes for a particular function to execute. Here is the code I am using to get familiar with time.h
The problem is, whenever I run it, the time elapsed is always 0. Is there something wrong in the code?
Code:
#include <stdio.h>
#include <time.h>
#include <iostream>
using namespace std;
double diffclock(clock_t clock1,clock_t clock2)
{
double diffticks=clock1-clock2;
double diffms=(diffticks*10)/CLOCKS_PER_SEC;
return diffms;
}
int main()
{
string name;
int i;
clock_t begin=clock();
cout << "Hi what is your name? ";
getline(cin, name);
clock_t end=clock();
cout << "Time elapsed: " << double(diffclock(end,begin)) << " ms"<< endl;
return 0;
}
The problem is, whenever I run it, the time elapsed is always 0. Is there something wrong in the code?