By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,106 Members | 1,081 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,106 IT Pros & Developers. It's quick & easy.

Please review the code for determining execution time.

P: n/a
Hi all,
I have written program for calculating the execution time of a
function.Any critics (on the method of calculating execution time) are
welcome.

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

#define DENO 10
typedef struct
{
int i;
float f;
char s;
double d;
long int li;
short si;
}str_t;

void foo(str_t str)
{
;
}

void foo1(str_t *str)
{
;
}
int main(void)
{
str_t str;
clock_t init_time,final_time;
double time_elapsed;

unsigned long int i;
init_time = clock();
for (i=0;i<ULONG_MAX/DENO;i++)
foo(str);
final_time = clock();

time_elapsed = (final_time - init_time)/CLOCKS_PER_SEC;

printf("Time elapsed when structure passed by value =
%f\n",time_elapsed);
init_time = clock();
for (i=0;i<ULONG_MAX/DENO;i++)
foo1(&str);
final_time = clock();

time_elapsed = (final_time - init_time)/CLOCKS_PER_SEC;

printf("Time elapsed when structure passed by address =
%f\n",time_elapsed);
return 0;
}

Jan 19 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
va******@rediffmail.com wrote:
Hi all,
I have written program for calculating the execution time of a
function.Any critics (on the method of calculating execution time) are
welcome.
Using execution profiler might be a better idea (e.g. GNU gprof).

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

#define DENO 10
typedef struct
{
int i;
float f;
char s;
double d;
long int li;
short si;
}str_t;

void foo(str_t str)
{
;
}

void foo1(str_t *str)
{
;
}
int main(void)
{
str_t str;
clock_t init_time,final_time;
double time_elapsed;

unsigned long int i;
init_time = clock();
for (i=0;i<ULONG_MAX/DENO;i++)
foo(str);
final_time = clock();

time_elapsed = (final_time - init_time)/CLOCKS_PER_SEC;

printf("Time elapsed when structure passed by value =
%f\n",time_elapsed);
init_time = clock();
for (i=0;i<ULONG_MAX/DENO;i++)
foo1(&str);
final_time = clock();

time_elapsed = (final_time - init_time)/CLOCKS_PER_SEC;

printf("Time elapsed when structure passed by address =
%f\n",time_elapsed);
return 0;
}


A few points on using clock():

- value returned may wrap around (you don't cater for this)
- it only gives you the _approximate_ CPU time elapsed
- on some implementations, the value returned also
includes "the times of any children whose status
has been collected via wait() (or another wait-type
call)." (from the man page on my system), so you'd have
to be careful in what's in your measured functions

I didn't look into your code in more detail.

Cheers

Vladimir

--
My e-mail address is real, and I read it.
Jan 19 '06 #2

P: n/a
Ico
va******@rediffmail.com wrote:
Hi all,
I have written program for calculating the execution time of a
function.Any critics (on the method of calculating execution time) are
welcome.
[snipped some code]
str_t str;
clock_t init_time,final_time;
double time_elapsed;

unsigned long int i;
init_time = clock();
for (i=0;i<ULONG_MAX/DENO;i++)
foo(str);
final_time = clock();

time_elapsed = (final_time - init_time)/CLOCKS_PER_SEC;


clock_t might be and integer type, and when all variables in this
expression are integers, the result will be integer as well. If you want
higher resolution, cast one of the variables to double :

time_elapsed = (final_time - init_time)/(double)CLOCKS_PER_SEC;

--
:wq
^X^Cy^K^X^C^C^C^C
Jan 19 '06 #3

P: n/a
Ico wrote:
va******@rediffmail.com wrote:
Hi all,
I have written program for calculating the execution time of a
function.Any critics (on the method of calculating execution time) are
welcome.

[snipped some code]

str_t str;
clock_t init_time,final_time;
double time_elapsed;

unsigned long int i;
init_time = clock();
for (i=0;i<ULONG_MAX/DENO;i++)
foo(str);
final_time = clock();

time_elapsed = (final_time - init_time)/CLOCKS_PER_SEC;

clock_t might be and integer type, and when all variables in this
expression are integers, the result will be integer as well. If you want
higher resolution, cast one of the variables to double :

time_elapsed = (final_time - init_time)/(double)CLOCKS_PER_SEC;

To state this more simply, you have written this function so as to
return the whole number of seconds. As you return a double, we suspect
that is not what you meant to do. Most current systems have at least
0.01 second resolution in clock(). In that case, your function result
would increase only after 100 ticks of clock(0).
Jan 19 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.