473,322 Members | 1,259 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Finding the time taken by a function

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
11 3223
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
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
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
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
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
You can also use gettimeofday function

Subhash

Nov 14 '05 #7
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
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
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


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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Graeme Longman | last post by:
Hi everyone, I was wondering if anyone has written some Python code which uses a start date and end date and a given interval (day, month or year) and outputs all the time periods for that range...
1
by: Simon Wadsworth | last post by:
My application uses VB6 WebClasses to handle the UI, so all requests come in via a stub ASP page. I would like to know the time taken for the request to be processed. I am trying to use the...
6
by: Mike | last post by:
Hi, I teach a VB .NET class, and one of the students obviously cheated on the final exam (which was to write a Tic Tac Toe game). They were smart enough to change all of the variables from the...
5
by: raybakk | last post by:
Hi there. If I make a function in c (I acually use gnu right now), is there any way to find out how many clocksycluses that function takes? If I divide some numbers etc Var1 = Var2/Var3, is it...
0
by: U S Contractors Offering Service A Non-profit | last post by:
This Sunday the 26th 2006 there will be Music @ Tue Nov Inbox Reply Craig Somerford to me show details 9:54 pm (26 minutes ago) #1St "CLICK" HeAt frOm A blanket...
4
by: mattG | last post by:
I have a scenario where I have these things that take up (x) number of space. I know the max number of space I will ever have is 512. I am trying to figure out a way to write a formula or...
0
by: elpy | last post by:
Hello, I am running a web site using tomcat 6.0 and am having a problem where tomcat's cpu utilization goes up to 95-97% and the response time for even simple page loads becomes very slow. If I use...
3
by: lucky33 | last post by:
My employer has asked me to create a database that will keep track of the employee attendance. Time off / Time earned, excused / unexcused, etc. At my company from the 6th month of employment to...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.