Calculate run time in milliseconds

Posted on 2006-11-16
Last Modified: 2010-05-18
I am running a C++ program that takes an NxN array and reads a
vertexCover. One requirement of this project is that it returns an
elapsed time of each function so the function efficiency can be
calculated based upon an input.

I am attempting to implement the time.h
header file and use 2 clock_t variables in order to calculate the
difference between the 2 times. Using for example t2-t1. When I return
this value I receive 0 seconds run time continually for each function.

I am looking for some way to output the run time of each function in
milliseconds to be able to calculate the fast run time without having to
input a large graph (1000+ nodes). Is there a multi-platform way to do
Question by:jmsloan

Expert Comment

ID: 17956163

 struct timeval tv1;
 struct timeval tv2;


 fprintf(stderr," time needed %i %i \n",tv2.tv_sec-otv1.tv_sec,tv2.tv_usec-otv1.tv_usec);

for windows check:

tv.tv_usec is in micro seconds,if you want to have mili, you must divide by 1000:

 fprintf(stderr," time needed %i %f \n",tv2.tv_sec-otv1.tv_sec,(tv2.tv_usec-otv1.tv_usec)/1000);
LVL 14

Accepted Solution

wayside earned 50 total points
ID: 17958395
For Windows, you can use the GetTickCount() function to get how many milliseconds have elasped since the system was started. You can call it multiple times and then subtract the values to get the elapsed time. (Note that in my experience, the difference between 2 successive calls will always by in multiples of 15 milliseconds or so, that is, for very short times you will get 0, and then the next lowest value you get will be 15 milliseconds. So it is not too precise for really small time intervals.

A little more work for Windows is to use the high-resolution timer (QueryPerformanceFrequency and QueryPerformanceCounter

LARGE_INTEGER time1, time2;



double elapsed = (double)(time2.QuadPart-time1.QuadPart)/(double)freq.QuadPart;
int ielapsed = elapsed * 1000000.0 + 0.5;  // elapsed time in microseconds.

LVL 24

Expert Comment

ID: 17961452
>  fprintf(stderr," time needed %i %f \n",tv2.tv_sec-otv1.tv_sec,(tv2.tv_usec-otv1.tv_usec)/1000);
is a bit of a problem if you don't do the carry properly.. there is a timeval_subtract function floating around which I am not sure who wrote it (I couldn't put the proper credit here) but I have a copy of it in my library.  So to borrow _iskywalker_ 's code:

#include <iostream>
#include <cstdlib>
#include <sys/resource.h>
using namespace std;

int timeval_subtract(struct timeval *result, struct timeval *x, struct timeval *y)
  if (x->tv_usec < y->tv_usec) {
    int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
    y ->tv_usec -= 1000000 * nsec;
    y->tv_sec += nsec;
  if (x->tv_usec - y->tv_usec > 1000000) {
    int nsec = (x->tv_usec - y->tv_usec) / 1000000;
    y->tv_usec += 1000000 * nsec;
    y->tv_sec -= nsec;
  result->tv_sec = x->tv_sec - y->tv_sec;
  result->tv_usec = x->tv_usec - y->tv_usec;

  return x->tv_sec < y->tv_sec;

struct timeval tv1, tv2, elapse;

timeval_subtract(&elapse, &(usage[1].ru_utime), &(usage[0].ru_utime)); // this is user time...
cout << "user\t" << double(elapse.tv_sec) + double(elapse.tv_usec)/1000000.0 << "sec" << endl;
timeval_subtract(&elapse, &(usage[1].ru_stime), &(usage[0].ru_stime)); // this is system time...
cout << "sys\t" << double(elapse.tv_sec) + double(elapse.tv_usec)/1000000.0 << "sec" << endl;

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Lambda for random numbers problem 7 118
FMX enumerated colours 2 119
How can i compile this github project?? 2 91
Adding items to a C# list incrementally 5 36
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question