Solved

Calculate run time in milliseconds

Posted on 2006-11-16
4
3,650 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
4 Comments
 
LVL 6

Expert Comment

by:_iskywalker_
Comment Utility

 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
Comment Utility
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
Comment Utility
>  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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now