![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Kshell scripts and timing | dbridle | AIX | 3 | 10-10-2006 01:26 PM |
| Timing out a SSH | rcunn87 | Shell Programming and Scripting | 9 | 07-31-2006 06:11 PM |
| scp timing out | jph | UNIX for Advanced & Expert Users | 1 | 06-09-2005 11:59 PM |
| bourne shell timing question | gillbates | UNIX for Dummies Questions & Answers | 7 | 02-01-2004 04:44 PM |
| timing a loop in unix. | wolkott | Shell Programming and Scripting | 1 | 02-04-2003 02:52 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
timing your functions
hi everyone. If you have a function created in your code and you want to find out how long it takes for it to run you can use a struct called gettimeofday().
so lets say we have a function like this int myfunction (int r) { /*some math calculations*/ return answer; } How do i set up gettimeofday to find out how long it takes for the function to run. thanks |
|
||||
|
Suggestion: use a profiler. Most UNIX systems compilers come with one.
Code:
cc myfile.c -o myfile -g -p ./myfile # run your code # prof ./myfile |
|
||||
|
Simplest way to obtain timing info if to run:
time ./a.out For info within program Id suggest using clock() instead of gettimeofday(). #include <time.h> clock_t t0, t1; t0=clock(); fun(); t1=clock(); printf("%f",(t1-t0)/CLOCKS_PER_SEC); Regards. |
|
||||
|
To use gettimeofday you can do this:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
static float getTimeDiff(struct timeval *s, struct timeval *e);
void main()
{
struct timeval sTv, eTv;
gettimeofday(&sTv, NULL);
/* do work */
gettimeofday(&eTv, NULL);
printf("Time difference is %.2f seconds\n", getTimeDiff(&sTv, &eTv));
}
Code:
static float getTimeDiff(struct timeval *s, struct timeval *e)
{
struct timeval diff_tv;
diff_tv.tv_usec = e->tv_usec - s->tv_usec;
diff_tv.tv_sec = e->tv_sec - s->tv_sec;
if (s->tv_usec > e->tv_usec)
{
diff_tv.tv_usec += 1000000;
diff_tv.tv_sec--;
}
return (float) diff_tv.tv_sec + ((float) diff_tv.tv_usec / 1000000.0);
}
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void main()
{
int i, j, t;
clock_t clockSample[3];
clockSample[0] = clock();
sleep(2);
clockSample[1] = clock();
for (i = 0; i < 1024; i++)
for (j = 0; j < 1024; j++)
t = i * i + j;
clockSample[2] = clock();
for (i = 0; i < 3; i++)
{
printf("Clock sample %d is %lu\n", i, clockSample[i]);
}
}
So, beware that both methods, while equally useful, answer two different questions. clock() tells you CPU time used, the gettimeofday method tells you actual time elapsed. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|