?
Solved

Calculate run time in milliseconds

Posted on 2006-11-16
4
Medium Priority
?
3,714 Views
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
this?
0
Comment
Question by:jmsloan
3 Comments
 
LVL 6

Expert Comment

by:_iskywalker_
ID: 17956163

 struct timeval tv1;
 struct timeval tv2;

gettimeofday(&tv1,NULL);
funciont();


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

for windows check:
http://www.winehq.org/pipermail/wine-devel/2003-June/018082.html

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);
0
 
LVL 14

Accepted Solution

by:
wayside earned 200 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;
QueryPerformanceCounter(&time1);

...

QueryPerformanceCounter(&time2);

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





0
 
LVL 24

Expert Comment

by:slyong
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;

gettimeofday(&tv1,NULL);
function();
gettimeofday(&tv2,NULL);
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;
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

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