# Measuring time elapsed in C++

• C/++/#

## Main Question or Discussion Point

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

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?

Related Programming and Computer Science News on Phys.org
KTC
Repeat action lots of time, take average time taken. Particularly for relatively simple function, a single call is going to take next to no time.

The following actually works with me:

Code:
#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;
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;
}
the only difference is that I didn't use strings, and that I multiplied with 1000 to get the true number of milliseconds. The actual resolution of clock() is about 15 ms.

Hmm, strange that is still always 0 ms.

rcgldr
Homework Helper
The actual resolution of clock() is about 15 ms.
I've used the following:

QueryPerformanceCounter()
Highest resolution timer, but includes overhead from other tasks. Still I found it to be the best. Using SetThreadPriority() to bump up priority should reduce overhead from other tasks.

GetProcessTimes()
Supposed to exclude overhead from other processes, but seems to include interrupt overhead. timeBeginPeriod() (requires a later timeEndPeriod()) can be used increase the tick rate, but this also seems to increase the reported time, indicating that interrupt overhead is included in the process times.

clock()
A more generic time call. On Windows XP, reports time in milliseconds, but is based on a 64hz ticker, 15.625ms.

Last edited:
D H
Staff Emeritus
ACLerok, do you notice the difference between your code and kamerling's code? There are 1000 milliseconds in a second, not ten.

Hurkyl
Staff Emeritus
Gold Member
clock() doesn't necessarily have anything to do with time as measured by the clock on the wall -- it measures the time used by your process.

So, I imagine that in ACLerok's operating environment, the I/O function hands control over to the system. The net effect being that while your program is waiting for I/O, that time is attributed to the system, and not your process. So, on your system, it is correctly reporting that the computer spent zero milliseconds in the process executing your program.

It's "working" for kamerling because, on that system, time waiting for I/O is being attributed to the calling process.

If you want to report time as measured by your clock on the wall, you'll have to use gettimeofday, or make use of some non-standard timing functions supplied by your compiler or C library (or other source). Jeff Reid has listed some of the timing functions in the Windows API. If you aren't using that, try checking your compiler's or C library's manual. (_rtc is a common extension)

And, incidentally, some of those headers do not exist in C++. You should be using
#include <cstdio>
#include <ctime>

Actually, you shouldn't include cstdio either, because you aren't using any of the standard C streaming I/O functions.

Last edited:
Ummm... Found this thread with google, and I wanted to say that the C/C++ standard library has a tool for this known as difftime that returns time change in seconds as a floating point double. Seconds was enough precision for my uses, maybe for yours.

Example based on yours:

Code:
#include <iostream>
#include <time.h>
#include <string>

int main()
{
[INDENT]using namespace std;
string name;
time_t begin, end;
time(&begin);
cout << "Hi what is your name? ";
getline(cin, name);
time(&end);
cout << "Time elapsed: " << difftime(end, begin) << " seconds"<< endl;
return 0;[/INDENT]
}
Anyways, there you go if you haven't figured it out in the two years since this thread was last updated.

Also, in case you want to add the difftime to a string, may want to include this little bit of code that allows floating point numbers to be written to a string:

Code:
#include <sstream>
template <typename datatype>
inline std::string write(datatype data)
{
[INDENT]std::ostringstream oStream;
oStream << data;
return oStream.str();[/INDENT]
}
An example of its use are as follows:

Code:
int main()
{
[INDENT]using namespace std;
double test_number = 3.14159; //anybody want some pi?
string test_string(write(test_number));
//Or
string append_test_string = ""; //initiate it as a blank string...
append_test_string.append(write(test_number));
//both print the same thing
cout << test_string << endl;
cout << append_test_string << endl;[/INDENT]
};
Be careful what you try to add though, some complex data types may not work with that.

Anyways, have fun.