LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-01-2011, 10:58 PM   #1
ejspeiro
Member
 
Registered: Feb 2011
Distribution: Ubuntu 14.04 LTS (Trusty Tahr)
Posts: 203

Rep: Reputation: 26
Question Timing a code: what's better?


Hallo!

This code computes a double integral using the Trapezoidal Rule:

PHP Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

/* Evaluate the required double integral by the use of the Trapezoidal Rule: */

double trap (  double aa,  /* Lower bound of x's interval. */
               
double bb,  /* Higher bound of x's interval. */
               
double cc,  /* Lower bound of y's interval. */
               
double dd,  /* Higher bound of y's interval. */
               
int NX,     /* Number of point to discretize x's interval. */
               
int NY);    /* Number of point to discretize y's interval. */

/* Evaluate the required integrand of the double integral, which at the same
    time is another integral that is going to be evaluated with the Trapezoidal
    Rule: */
    
double feval (  double xx,  /* Function argument (mathematically speaking). */
                
double hh,  /* Step size for local Trapezoidal Rule. */
                
double cc,  /* Lower bound of y's interval. */
                
double dd,  /* Higher bound of y's interval. */
                
int NN);    /* Number of point to discretize y's interval. */

/* Evaluate the actual function F(xx, yy) that stands for the
    "integrand of the integrand" of the double integral. This is also useful
    because we can easily try for different test cases just by changing this
    module. */
    
double IntegrandFF (double xxdouble yy);

int main (void) {

  
double aa;      /* Lower bound of x's interval. */
  
double bb;      /* Higher bound of x's interval. */
  
double cc;      /* Lower bound of y's interval. */
  
double dd;      /* Higher bound of y's interval. */
  
double result;  /* Result of the entire integration. */
  
int NX;         /* Number of point to discretize x's interval. */
  
int NY;         /* Number of point to discretize y's interval. */

  /* Parameters of the problem: */
  
aa 0.0;
  
bb M_PI;
  
cc 0.0;
  
dd M_PI;
  
NX 10000;
  
NY 10000;

  
result trap (aabbccddNXNY);
  
printf("The resulting double integration equals %lf\n"result);
  return 
EXIT_SUCCESS;
}

/* Evaluate the actual function F(xx, yy) that stands for the
    "integrand of the integrand" of the double integral. This is also useful
    because we can easily try for different test cases just by changing this
    module. */
    
double IntegrandFF(double xxdouble yy) {

  return 
sin(xx)*sin(yy);
}


/* Evaluate the required integrand of the double integral, which at the same
    time is another integral that is going to be evaluated with the Trapezoidal
    Rule: */
    
double feval (double xxdouble hydouble ccdouble ddint NN) {

  
double yy;        /* Spatial variable per each iteration. */
  
double acum_01d;  /* Auxiliary accumulation variable. */
  
unsigned int ii;  /* Iterator. */
  
  
yy cc;
  
acum_01d 0.0;
  for (
ii 1ii NNii++) {
    
yy += hy;
    
acum_01d += IntegrandFF(xxyy);
  }  
  return 
hy*(0.5*(IntegrandFF(xxcc) + IntegrandFF(xxdd)) + acum_01d);
}

/* Evaluate the required double integral by the use of the Trapezoidal Rule: */

double trap (double aadouble bbdouble ccdouble ddint NXint NY) {

  
double hx;        /* Grid spacing for x. */
  
double hy;        /* Grid spacing for y. */
  
double xx;        /* Spatial variable per each iteration. */
  
double acum_01d;  /* Auxiliary accumulation variable. */
  
unsigned int ii;  /* Iterator. */

  
printf("We will integrate from %f to %f and from %f to %f\n"aabbccdd);
  
printf("We will integrate using %i points for x and %i for y\n"NXNY);

  
/* Compute the grid spacing values: */
  
hx = (bb aa)/NX;
  
hy = (dd cc)/NY;
  
printf("That yields to hx = %f and hy = %f\n"hxhy);
  
  
xx aa;  
  
acum_01d 0.0;
  for (
ii 1ii NXii++) {
    
xx += hx;
    
acum_01d += feval(xxhyccddNY);
  }
  return 
hx*(0.5*(feval(aahyccddNY) + feval(bbhyccddNY))
    + 
acum_01d);

I would like to time the execution of the code using time.h.

Specifically the time the function trap() takes to execute. What would be better? To start/finish the timing outside the routine or to do it inside of it?

I would like to time it from the source code, i.e. not using external software such as the UNIX function time or profilers.

Also, I would like to time it in seconds with precission of milliseconds.

Any other related comments to timing code in ANSI C?

Please, feel free to "destroy" my code, in the sense of criticize it!

Best regards to everyone!

Last edited by ejspeiro; 03-01-2011 at 11:29 PM.
 
Old 03-03-2011, 08:25 AM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by ejspeiro View Post
What would be better? To start/finish the timing outside the routine or to do it inside of it?
Why would that decision matter enough to ask the question? Do whichever is more convenient for you.

What about the printf's inside trap() do you want those included in the measured time or not?

I expect you are measuring the performance with NX*NY quite large, so including or excluding the printf's and/or calling overhead might not make a significant difference.

Quote:
I would like to time the execution of the code using time.h
time.h declares/defines various functions and structures. Are you asking which of those functions you ought to use?

Quote:
Also, I would like to time it in seconds with precission of milliseconds.
There is a rather confusing array of related functions to choose from for getting time info. gettimeofday() is the cosest I found to fitting your needs. But that does mean you need to convert from the microseconds it returns to the milliseconds, and you need to combine the separate whole seconds it returns with the computed milliseconds.

Last edited by johnsfine; 03-03-2011 at 08:27 AM.
 
Old 04-19-2011, 10:32 AM   #3
ejspeiro
Member
 
Registered: Feb 2011
Distribution: Ubuntu 14.04 LTS (Trusty Tahr)
Posts: 203

Original Poster
Rep: Reputation: 26
johnsfine:

Sorry I never actually got to reply to this.

I read your post and I must confess that I realize now that my original posting was really confusing... sorry for that.

However I would like to thank you for your reply.

Regards!
 
  


Reply

Tags
[c]



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Cron Timing Murdock1979 Linux - Server 4 09-27-2011 04:02 PM
timing in c adixon Programming 11 11-16-2010 08:15 PM
how to start timing and print the timing result on portions of java codes ?? alred Programming 2 05-15-2006 10:00 AM
User Preferences: Use HTML code instead of vB code? (vB code is overrated) stefanlasiewski LQ Suggestions & Feedback 5 07-26-2005 01:37 AM
timing in c++ deveraux83 Programming 2 04-20-2004 05:34 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration