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