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

Please review the code for determining execution time.

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
3 2164
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: laurenq uantrell | last post by:
I have been using the following function to test the speed of various functions, however, quite often the return value is zero. I'm hoping someone can help improve on this. Function TimeIt() As...
0
by: Ammar | last post by:
Hi ... I have been reading throuhg the internet to solve a problem that happens to me when i try to access a databse in SQL mode... when i access the database through (visual web developer 2005)...
7
by: Drew Berkemeyer | last post by:
I've encounted a pretty strange problem and I'm not quite sure what to make of it. I have a web service that consumes an XML file as well as a few other parameters. This web service works fine...
3
by: wASP | last post by:
Hi, Apparently, I have a permissions issue. I am logged in as administrator, and my C: drive is accessible. Under the "Security" tab for Properties for Inetpub, for my "Internet Guest Account,"...
0
by: Jimmy | last post by:
I have a web page that displays data from a sql server db. I'm using a stored procedure to return data from a field of type text as an OUTPUT parameter. How can I use an OUTPUT parameter of...
10
by: Rob Dob | last post by:
Hi, I'm amazed!!! I am using VS2005, I create a new web project, c# and then within the default.aspx form right mouse click and select "View Compnent Designer" , I then select and drag a...
22
by: KitKat | last post by:
I need to get this to go to each folders: Cam 1, Cam 2, Cam 4, Cam 6, Cam 7, and Cam 8. Well it does that but it also needs to change the file name to the same folder where the file is being...
1
by: Brad Isaacs | last post by:
I am working with ASP.NET 2.0 and using an SQL Server 2000 database. I am using Visual Studio 2005 and developing on my Local machine. I am working with Login controls ASP.Configuration, I...
4
by: Brad Isaacs | last post by:
I am working with ASP.NET 2.0 and using an SQL Server 2000 database. I am using Visual Studio 2005 and developing on my Local machine. I am working with Login controls ASP.Configuration, I...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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...

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.