473,792 Members | 3,005 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_MA X/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_elap sed);
init_time = clock();
for (i=0;i<ULONG_MA X/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_elap sed);
return 0;
}

Jan 19 '06 #1
3 2194
va******@rediff mail.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_MA X/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_elap sed);
init_time = clock();
for (i=0;i<ULONG_MA X/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_elap sed);
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******@rediff mail.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_MA X/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******@rediff mail.com wrote:
Hi all,
I have written program for calculating the execution time of a
function.An y 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_MA X/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
7043
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 Single On Error GoTo CodeErr 'PURPOSE: Times a process in seconds from start to finish 'USAGE: msgbox TimeIt Dim sngStart As Single
0
936
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) ie testing it, then everything runs perfect! but then i simply connected the database to the page by draging the table to the ASP.NET page and the web developer creates the necessary code... it seems that the page access the Database using windows...
7
2675
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 if I use the web test interface. It also works fine if I call it from an ASP.NET page that has a text box where the XML is pasted and then passed on. However, I get an exception if I use an <input type="file"> control on the ASP page that allows...
3
1328
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," the "Allow" boxes for , , and are checked - BUT they are greyed out - I cannot clear them - and nothing else is checked. Now that I look further, the same thing is the case for every folder
0
1296
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 datatype text being returned from a stored procedure? I need to use it's value to display comments text on a profile page. The comments will be more than 8000 characters so using varchar is not an option. Maybe I'm on the wrong track...if so, then...
10
2067
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 SQLDataAdapter onto the designer. I then go through the wizard and create SQL Query. I then compile the project, I have add in a "InitializeComponent();" within the Page_Load, I then run the program. everything works okay,, as it does noting. ...
22
2203
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 grabbed, BUT it doesn't. I have tried and tried.....please help example: C:\Projects\Darryl\Queue Review Files\2-24\Cam 7\Cam7-20060224170000-01.jpg Cam7 but all I keep getting is Cam1, as the beginning of the jpg name,...:( HELP!
1
3760
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 wanted to move my work and needed to place it on the server. Using VS 2005 , went to BUILD -Publish Web Site Checked the box for :: Alow this precompiled site to be updatable.
4
2216
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 wanted to move my work and needed to place it on the server. Using VS 2005 , went to BUILD -Publish Web Site Checked the box for :: Alow this precompiled site to be updatable.
0
9670
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9518
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9033
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5560
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4111
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3719
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2917
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.