473,701 Members | 2,603 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 1816
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
2194
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 server). I appreciate ur help.
8
3461
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 As String, ByVal lpInitData As String) As Integer
0
839
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
1996
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, I can match the new time to the old time and get the difference in order to measure the...
13
2758
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; http://www.research.att.com/~bs
0
1799
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, I have no
0
1237
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 C++?
1
2755
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 me out... int Encrypt (word8 a, word8 rk) { // Encryption of one blockl int r; sec_init();...
1
1506
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 the compiler will not fold the loop.
0
8736
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9229
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9083
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8977
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8934
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6571
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5904
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4410
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3102
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.