473,385 Members | 1,402 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,385 software developers and data experts.

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 1792
On 4 May 2005 07:23:10 -0700, cr**********@gmail.com
<cr**********@gmail.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_PER_SEC;

printf("processor 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_PER_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_PER_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*********@sun.com> wrote:


Chris Croughton wrote:

clock_t start, end;
time_t tstart, tend;
double t;
[...]
t = (end - start) / (float)CLOCKS_PER_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*********@sun.com> wrote:


Chris Croughton wrote:
clock_t start, end;
time_t tstart, tend;
double t;
[...]
t = (end - start) / (float)CLOCKS_PER_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).


*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
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...
8
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?...
0
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
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...
13
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...
0
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 ,...
0
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: ...
1
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...
1
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...
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...
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.