Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Timing a function

Status
Not open for further replies.

abp

Programmer
Sep 14, 2000
134
FR
Hi All

My friend has this problem where he needs to measure the time spent inside
a function in *Milliseconds*. I cant find any libary which allows measuring of time in
milliseconds ? The best is to use the following code

#include <time.h>

static time_t start=NULL;
static time_t stop=NULL;

#define START time(&start)
#define STOP time(&stop)

static void get_time()
{
printf(&quot;Time elapsed is %.6lf&quot;, difftime(stop, start));
}

START;
function_to_time();
STOP;
get_time();

This code is able to return elapsed time in seconds. Since the function of interest takes much
lesser time than 1 sec, the accuracy has to be in milliseconds.

Any help is appreciated.

Thanks in advance

Anand :cool:
 
Hi !

I found that the following code does the work.

static clock_t begin=NULL;
static clock_t end=NULL;

static void start()
{
begin = clock();
}

static void stop()
{
end = clock();
}


#define START start()
#define STOP stop()

static void get_time()
{
var_nextmsg(&quot;The time taken for the function is %lf milliseconds\n&quot;, 1000.0*(end-begin)/CLOCKS_PER_SEC);
}

Thanks for help by all!

Anand


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top