473,545 Members | 2,085 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

tool to measure performance of written function

hello,
If i wrote a C function then how to know that its costlier
in terms of processing time,execution, compilation.
I have written 2 functions for one problem but dont understand which
one is best from Opereating System's overhead point of view. so i want
tool that measure it.
If this is not right place to ask then tell me where to ask in any
other forum.

Nov 14 '05 #1
6 1808
On 4 May 2005 07:23:10 -0700, cr**********@gm ail.com
<cr**********@g mail.com> wrote:
hello,
If i wrote a C function then how to know that its costlier
in terms of processing time,execution, compilation.
I have written 2 functions for one problem but dont understand which
one is best from Opereating System's overhead point of view. so i want
tool that measure it.
If this is not right place to ask then tell me where to ask in any
other forum.


#include <stdio.h>
#include <time.h>

/* declare my_function() */

#define LARGE_NUMBER 1000000

int main(void)
{
clock_t start, end;
time_t tstart, tend;
double t;
long i;

tstart = time(NULL);
start = clock();

for (i = 0; i < LARGE_NUMBER; ++i)
my_function();

end = clock();
tend = time(NULL);

t = (end - start) / (float)CLOCKS_P ER_SEC;

printf("process or time %g us, elapsed %g us\n",
t * 1e6 / LARGE_NUMBER,
difftime(tend, tstart) * 1e6 / LARGE_NUMBER);
}

This will give the times per invocation of the function.

Then repeat it with your other function instead of my_function().

You'll need to tweak the value of LARGE_NUMBER so that you get a
reasonable granularity, typically you'll want an execution time of at
least 100 seconds to get 2-digit resolution of the elapsed time (the
processor time returned by clock() is typically an order of magnitude or
better more accurate).

Note that on some systems clock() or time() might not work, or might be
of little use because of insufficient resolution (time() is often in
seconds, clock() is often in milliseconds but may only be updated 20
or fewer times a second). The elapsed time can also be affected by what
other processes (tasks) are running.

If you want to know the compilation time, get out a stopwatch, or ask in
a newsgroup about your operating system, that's outside standard C. If
you want to know how much time is spent in system functions, you
definitely need to ask in a newsgroup relevat to your system...

Chris C
Nov 14 '05 #2


Chris Croughton wrote:

clock_t start, end;
time_t tstart, tend;
double t;
[...]
t = (end - start) / (float)CLOCKS_P ER_SEC;


(Drifting slightly): Why `float' and not `double'?

In fact, there's even a faint possibility that
CLOCKS_PER_SEC could be `long double', so the cast to
mere `double' could lose precision. I'd suggest
writing

t = (end - start) / (CLOCKS_PER_SEC + 0.0);

as a way of ensuring that the calclulation is done in
"double or better" -- and if you really, truly wanted
only "float or better," you could write

t = (end - start) / (CLOCKS_PER_SEC + 0.0f);

Casting the overall result to `double' before the
assigment is optional, and might keep some compilers
from issuing warning diagnostics -- again, in the
unlikely event that CLOCKS_PER_SEC and/or `time_t' is
`long double'.

--
Er*********@sun .com

Nov 14 '05 #3
On Wed, 04 May 2005 14:13:20 -0400, Eric Sosman wrote:


Chris Croughton wrote:

clock_t start, end;
time_t tstart, tend;
double t;
[...]
t = (end - start) / (float)CLOCKS_P ER_SEC;


(Drifting slightly): Why `float' and not `double'?

In fact, there's even a faint possibility that
CLOCKS_PER_SEC could be `long double', so the cast to
mere `double' could lose precision. I'd suggest
writing

t = (end - start) / (CLOCKS_PER_SEC + 0.0);


However precision beyond double may not be important. You would also need
to make t long double to make use of it.

Lawrence

Nov 14 '05 #4
On Wed, 04 May 2005 14:13:20 -0400, Eric Sosman
<er*********@su n.com> wrote:


Chris Croughton wrote:

clock_t start, end;
time_t tstart, tend;
double t;
[...]
t = (end - start) / (float)CLOCKS_P ER_SEC;
(Drifting slightly): Why `float' and not `double'?


Since I had double t; no real reason (except that it saves a character
typing <g>).
In fact, there's even a faint possibility that
CLOCKS_PER_SEC could be `long double', so the cast to
mere `double' could lose precision. I'd suggest
writing

t = (end - start) / (CLOCKS_PER_SEC + 0.0);
Given the precision of CLOCKS_PER_SEC on most machines, I don't see the
point. You'd be into sub-microsecond accuracy before you ran off the
end of a float, and sub-nanosecond with a double (that's with the lowest
precision for float and double allowed by the standard).

Better would be to do

t = (double)(end - start) / CLOCKS_PER_SEC;

and avoid later programmers saying "but adding zero doesn't do anything
so I'll take it out" -- making the cast explicit indicates to
maintainers that there was a reason for it.
as a way of ensuring that the calclulation is done in
"double or better" -- and if you really, truly wanted
only "float or better," you could write

t = (end - start) / (CLOCKS_PER_SEC + 0.0f);
Again, I would rather use an explicit cast if I wanted it, it's more
obvious what it's doing.
Casting the overall result to `double' before the
assigment is optional, and might keep some compilers
from issuing warning diagnostics -- again, in the
unlikely event that CLOCKS_PER_SEC and/or `time_t' is
`long double'.


Indeed. I think I'll deal with that warning if it ever arises (many
programmers think that putting in casts to avoid warnings is bad anyway,
putting them in to avoid a possible warning which is unlikely to happen
is really going to annoy them)...

Chris C
Nov 14 '05 #5


Chris Croughton wrote:
On Wed, 04 May 2005 14:13:20 -0400, Eric Sosman
<er*********@su n.com> wrote:


Chris Croughton wrote:
clock_t start, end;
time_t tstart, tend;
double t;
[...]
t = (end - start) / (float)CLOCKS_P ER_SEC;
(Drifting slightly): Why `float' and not `double'?

Since I had double t; no real reason (except that it saves a character
typing <g>).

In fact, there's even a faint possibility that
CLOCKS_PER_SE C could be `long double', so the cast to
mere `double' could lose precision. I'd suggest
writing

t = (end - start) / (CLOCKS_PER_SEC + 0.0);

Given the precision of CLOCKS_PER_SEC on most machines, I don't see the
point. You'd be into sub-microsecond accuracy before you ran off the
end of a float, and sub-nanosecond with a double (that's with the lowest
precision for float and double allowed by the standard).


*Practical* arguments? *Real-world* reasons?! Hast
thou forgotten that thou'rt on comp.lang.c, where we worry
about things like padding bits in `short', eleven-bit `char',
and NULLs that aren't all-bits-zero? Pfui! Avaunt ye, thou
scalawag; thou'rt unworthy to be called Pedant! ;-)
[...]
Indeed. I think I'll deal with that warning if it ever arises (many
programmers think that putting in casts to avoid warnings is bad anyway,
putting them in to avoid a possible warning which is unlikely to happen
is really going to annoy them)...


I once encountered a compiler that warned about
"possible loss of precision" for `float f = 0.0;', and
actually had to turn the change-approval crank to "fix"
it with `float f = 0.0f;' ...

--
Er*********@sun .com

Nov 14 '05 #6
Eric Sosman wrote:

Chris Croughton wrote:
Given the precision of CLOCKS_PER_SEC on most machines,
I don't see the
point. You'd be into sub-microsecond accuracy before
you ran off the
end of a float, and sub-nanosecond with a double
(that's with the lowest
precision for float and double allowed by the standard).


*Practical* arguments?


double is my default choice for floating point type.
I would need a special reason to use either float or long double.
I once encountered a compiler that warned about
"possible loss of precision" for `float f = 0.0;', and
actually had to turn the change-approval crank to "fix"
it with `float f = 0.0f;' ...


I like that warning.
I prefer to be mindful of expression types
and to match them up with each other whenever appropriate.

--
pete
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2190
by: Puri Malluru | last post by:
Hi, We have a slow web application. I am wondering whether there is any tool for measuring performance. Like when I am downloading a page, how much time the browser is spending downloading each component/sub files on that page. The browser is IE. The web server is custom built server running on embedded box (not IIS or any Apache type...
8
3440
by: gregory_may | last post by:
Is there a way to grab a "Screen Shot" that includes "Tool Tips"? I saw this code someplace, cant remember where. But it doesnt grab "Tool Tips". Is there a better way to do this in .net? Thanks! Private Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" (ByVal lpDriverName As String, ByVal lpDeviceName As String, ByVal lpOutput...
0
830
by: Silent Ocean | last post by:
Do we have any tools available to measure the performance of a dot net c# application in multiuser scenario??
2
1989
by: Bond | last post by:
Hi! I'd like to measure the performance of my web page. I'd like to set a timer on the page to start when a user clicks on a link, or submit... anything where the browser requests a page from the server. The idea would be to set a hidden form variable to the current time when an action occurs, then when the page from the server is returned,...
13
2734
by: bjarne | last post by:
Willy Denoyette wrote; > ... it > was not the intention of StrousTrup to the achieve the level of efficiency > of C when he invented C++, ... Ahmmm. It was my aim to match the performance of C and I achieved that aim very early on. See, for example "The Design and Evolution of C++". -- Bjarne Stroustrup;...
0
1794
by: CharlesA | last post by:
sorry I had to clarify my previous post as I wasn't able to get specific answers so I'm made this very targetted to what I actually pretty desperate to fix. my Setup: I've got XP pro, IIS 5 , visual Studio 2005, asp.net 2.0 and C#, I have SQL server 2005 installed as well. if I create a web site with the internal web server of VS2005,...
0
1232
by: Gus007 | last post by:
Hi all, I was wondering how I could measure performance in my C++ program.I need to do this analysis for my university final project. Perfromance For performance I believe I could use: * Time consumed in each of the functions. --> maybe using system time before and after the function executiuon.Any suggestions on how to do that in...
1
2750
by: kartouss | last post by:
Hello i am new to this forum...can you help me solve this problem.. I have a function encrypt() which encrypts data from a file in 16 bytes till end of file which in turn calls other 4 functions...and i need to measure the time it encrypts the data consecutively for all the 4 functions till end of file... I am fwding you the codes...Please help...
1
1494
by: Dijkstra | last post by:
On 9 jul, 15:16, "gautamcoo...@gmail.com" <gautamcoo...@gmail.com> wrote: The compiler does not "execute" nothing. But I understand what you mean. The compiler only optimizes what it can see at compile time, nothing more, nothing less. You can separate your target function and the loop in two distinct modules, so you will be pretty sure...
0
7468
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7401
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7656
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7757
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5972
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4945
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3450
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1014
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
704
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.