Please review the code for determining execution time.

V

vashwath

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;
}
 
V

Vladimir S. Oka

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
 
I

Ico

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;
 
T

Tim Prince

Ico said:
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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top