?
Solved

Calculate run time in milliseconds

Posted on 2006-11-16
4
Medium Priority
?
3,707 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 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

649 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