By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,985 Members | 1,712 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,985 IT Pros & Developers. It's quick & easy.

Finding the time taken by a function

P: n/a
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.

Nov 14 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
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

<sr*******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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.

Nov 14 '05 #2

P: n/a
Wow, that was a bad typo, precision.

"Yaniv Oliver" <yaniv (at) softlink.com> wrote in message
news:41********@news.012.net.il...
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

<sr*******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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.


Nov 14 '05 #3

P: n/a
Wow, you both top post, which is a Bad Thing.

Please stop!

"Yaniv Oliver" <yaniv (at) softlink.com> wrote in message
news:41********@news.012.net.il...
Wow, that was a bad typo, precision.

"Yaniv Oliver" <yaniv (at) softlink.com> wrote in message
news:41********@news.012.net.il...
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

<sr*******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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.



Nov 14 '05 #4

P: n/a
sr*******@gmail.com wrote:
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.
Nov 14 '05 #5

P: n/a
On Mon, 24 Jan 2005 11:50:46 +0200, Yaniv Oliver wrote:
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

Nov 14 '05 #6

P: n/a
You can also use gettimeofday function

Subhash

Nov 14 '05 #7

P: n/a
On 24 Jan 2005 05:41:19 -0800, in comp.lang.c , "Subhash"
<su************@gmail.com> wrote:
You can also use gettimeofday function


Thats not a c function, its a posix one, and may not be available. Besides,
it would be offtopic here.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
Nov 14 '05 #8

P: n/a
Lawrence Kirby <lk****@netactive.co.uk> wrote:
On Mon, 24 Jan 2005 11:50:46 +0200, Yaniv Oliver wrote:
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.


[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).

--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Nov 14 '05 #9

P: n/a
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).

Nov 14 '05 #10

P: n/a


Sanda wrote:
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

--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #11

P: n/a
Sanda wrote:
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 <stdio.h>
//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.

--
"Women should come with documentation." - Dave
Nov 14 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.