Solved

Calculate run time in milliseconds

Posted on 2006-11-16
4
3,688 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 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 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;
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
  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 …
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 learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

738 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