Finding the time taken by a function

S

srkkreddy

Hi,

I have written a large program which makes multiple calls to number of
functions (also written by me) of the program. Now, I want to know the
collective time taken by all the calls to a specific function. Is
there any way I can find this time.
Thanks in advance,
Raja Kiran Sandireddy.
 
Y

Yaniv Oliver

If you don't care how accurate the results are (and don't need better
precession than just seconds), you can call time() before you call the
function you want to time, and then after it returns call time() again and
subtract the values.

Yaniv
 
Y

Yaniv Oliver

Wow, that was a bad typo, precision.

Yaniv Oliver said:
If you don't care how accurate the results are (and don't need better
precession than just seconds), you can call time() before you call the
function you want to time, and then after it returns call time() again and
subtract the values.

Yaniv
 
K

Karthik Kumar

Hi,

I have written a large program which makes multiple calls to number of
functions (also written by me) of the program. Now, I want to know the
collective time taken by all the calls to a specific function. Is
there any way I can find this time.
Thanks in advance,
Raja Kiran Sandireddy.

You are essentially profiling the application. Depending on your
implementation, you can use an appropriate profiler.

On most gnu systems, you can use gprof for profiling.
 
L

Lawrence Kirby

If you don't care how accurate the results are (and don't need better
precession than just seconds), you can call time() before you call the
function you want to time, and then after it returns call time() again and
subtract the values.

clock() tends to be better suited for measuring CPU time. However even
that will have limited resolution so quick functions will probably end up
as 0 difference most of the time. As mentioned elsewhere a profiler would
be a good placed to start.

Lawrence
 
S

S.Tobias

clock() tends to be better suited for measuring CPU time. However even
that will have limited resolution so quick functions will probably end up
as 0 difference most of the time.

[OT]
If you assume that a computer is a stochastic machine, ie. function calls
are delayed in random fashion (I think it is a safe bet for ordinary
desktop computers - it's enough to move your mouse every now and then),
then statistically you could measure the time a function is executed
which is below the resolution of the time measurement if you have
enough samples (in theory at least, but it worked for me in the past
in another language).
 
S

Sanda

You can use the clock() function to calculate the time. Just call it
before and after the function calls and substract the two values and
devide it buy CLK_TCK which is the systems pulse time.

eg:
#include<time.h>
//other include files

void main()
{clock_t start, end;

//codes................

start=clock();
//your funcrion calls
end =clock();
printf("\n Time taken is %f seconds.\n",(end-start)/CLK_TCK);


}

for more information use the help files in your compiler. (I used the
Turbo c++ compiler for this).
 
A

Al Bowers

Sanda said:
You can use the clock() function to calculate the time. Just call it
before and after the function calls and substract the two values and
devide it buy CLK_TCK which is the systems pulse time.
CLK_TCK is not defined in Standard C. If you are writing Stanard
C code you should use the defined macro CLOCKS_PER_SEC which is a
clock_t type that represents the number per second of the value
returned by the clock function. The clock_t type is an arithmetic
type capable of representing times; and whose range and precision
of times is implementation-defined.
SEE: http://www.eskimo.com/~scs/C-faq/q19.37.html
eg: For a standard C code:
#include<time.h>
//other include files
Yes, you need stdio.h
void main()
int main(void)
SEE: http://www.eskimo.com/~scs/C-faq/q11.15.html
{clock_t start, end;

//codes................

start=clock();
//your funcrion calls
end =clock();
printf("\n Time taken is %f seconds.\n",(end-start)/CLK_TCK);

Use CLOCKS_PER_SEC as mentioned above.
return 0;
}

for more information use the help files in your compiler. (I used the
Turbo c++ compiler for this).
For this NG, the absolute resource is the ANSI C STANDARD.
SEE: http://www.eskimo.com/~scs/C-faq/q11.2.html
 
J

Jonathan Burd

Sanda said:
You can use the clock() function to calculate the time. Just call it
before and after the function calls and substract the two values and
devide it buy CLK_TCK which is the systems pulse time.

eg:
#include<time.h>

#include said:
//other include files

void main()

UB. int main()
{clock_t start, end;

style?

//codes................

start=clock();
//your funcrion calls
end =clock();
printf("\n Time taken is %f seconds.\n",(end-start)/CLK_TCK);

without stdio.h, UB.

printf (
"\n Time taken is %f seconds.\n",(double)(end-start)/CLOCKS_PER_SEC
);

return 0;
for more information use the help files in your compiler. (I used the
Turbo c++ compiler for this).

The Turbo C++ compiler is a C++ compiler. Use GCC or something
that supports the C Standard.

/*
* @file: interval.c
*/
#include <stdio.h>
#include <limits.h>
#include <time.h>

int
main (int argc, char **argv)
{
clock_t start, /* start of timing */
end; /* end of timing */
int i; /* index */

/* (A)
* Start timing
*/
start = clock ();

/* (B)
* the code to time goes here.
* sample follows (warning:
* this may take a long time!):
*/
for (i = 0; i < INT_MAX; ++i)
printf ("%d\r", i);

/* (C)
* end timing
*/
end = clock ();

/* (D)
* display interval
*/
printf ("\nInterval: %.2f\n", (double)(end-start)/CLOCKS_PER_SEC);

return 0;
}

Regards,
Jonathan.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top