comp.lang.ada
 help / color / mirror / Atom feed
From: Constantine Antonovich: <const@orbotech.co.il>
Subject: Re: Fortran vs C++ vs. etc (has little to do with realtime anymore)
Date: 1997/09/25
Date: 1997-09-25T00:00:00+00:00	[thread overview]
Message-ID: <342A1486.617C@orbotech.co.il> (raw)
In-Reply-To: vi8oh5keyy1.fsf@drabble.fnal.gov


Oleg Krivosheev wrote:
> 
> [skipped]
> 
> sorry, i have only min ;)
> 
> well, try example below for min with
> arbitrary larger number of arguments. Hope it can help
> 
> #include <assert.h>
> #include <stdio.h>
> #include <stdarg.h>
> 
> double
> dmin( int nofargs, ... ) {
> 
>   double res = 1.0e+38, arg;
>   int j;
>   va_list ap;
> 
>   assert( nofargs > 1 );
> 
>   va_start(ap, nofargs );
> 
>   for( j = 0; j < nofargs; ++j ) {
>     arg=va_arg( ap, double );
>     if ( arg < res ) {
>       res = arg;
>     }
>   }
>   va_end(ap);
>   return res;
> }
> 
> int
> main( void ) {
> 
>   printf( "%e\n", dmin( 2, 1.0, 2.0 ) );
> 
>   printf( "%e\n", dmin( 3, 0.0, 1.0, 2.0 ) );
> 
>   printf( "%e\n", dmin( 5, 0.0, 1.0, 2.0, 7.0, -0.2 ) );
> 
>   return 0;
> }
> 
> [skipped]
> 
    OK, just to be pedantic, the previous  example shows very fine
the  difference  between  fortran and  C    in computations. Since
min/max are  standard fortran functions, it  can be  supposed they
are maximally   optimized  with no  regards  to   number  of their
arguments (just because the compiler takes care of them).
    In the  contrary,   in the example,  we  have overhead of  the
arguments  number  evaluation and overhead of  implementation (not
necessarily    this   one,  generally   almost    any  non-trivial
non-built-in operation has  some overhead -   there has been  some
reason   to  add  into  C  libraries  memcpy,   memcmp  and so  on
functions).
    Moreover the idea of such an implementation contains two great
places   to  make   bugs and definitely     any  user  of  such an
implementation will be  doing the bugs  endlessly. Firstly, it  is
the  programmer who should worry about  conformity of the declared
number of  arguments to the actual  one. In such cases, generally,
adding or  removing  actual arguments, people  tend   to forget to
correct the  declared number. Secondly,  there is no  way for type
checking and  for type conversions, usage  of 1 instead of  1.0 in
the arguments list will cause very nasty problems.
    Using  C++ and not C, most  probably, I would try to implement
the min function in the following way:

inline double min(double v1, double v2) {
  return v1<v2 ? v1 : v2;
}
inline double min(double v1, double v2, double v3) {
  double v=min(v1,v2); return v<v3 ? v : v3;
}
inline double min(double v1, double v2, double v3, double v4) {
  double v=min(v1,v2,v3); return v<v4 ? v : v4;
}
inline double min(double v1, double v2, double v3, double v4, double v5)
{
  double v=min(v1,v2,v3,v4); return v<v5 ? v : v5;
}
// and so on

    Such  a way  doesn't seem elegant enough and it only  emulates
arbitrary number of min-function arguments but it solves the types
and the  number-conformity problems.   Trying to  use  non-defined
min(huge-arguments-number),   a programmer  just gets  compilation
error and can easily extend the  set of his min-functions. And the
last but not least: the following example compiled with -O3 option
with Sparc C++ 4.2 patch 104631-03 and
  started  on SunOS 5.5.1 SUNW,Ultra-2 produces
    dmin: 3.04 seconds
    overloaded min: 1.11 seconds
  started on SunOS 5.5.1 SUNW,SPARCstation-4 produces:
    dmin: 9.32 seconds
    overloaded min: 4.89 seconds
and compiled  with -O option with HP  C++  A.03.72; and started on
HP-UX A.09.05 9000/715 produces:
    dmin: 4.63 seconds
    overloaded min: 1.58 seconds

#include <sys/times.h>
#include <limits.h>
#include <iostream.h>

extern double dmin(int, double, ...);
//------------------------------------------------------
inline double min(double v1, double v2) {
  return v1<v2 ? v1 : v2;
}
inline double min(double v1, double v2, double v3) {
  double v=min(v1,v2); return v<v3 ? v : v3;
}
inline double min(double v1, double v2,
		  double v3, double v4) {
  double v=min(v1,v2,v3); return v<v4 ? v : v4;
}
inline double min(double v1, double v2,
		  double v3, double v4, double v5) {
  double v=min(v1,v2,v3,v4); return v<v5 ? v : v5;
}
//------------------------------------------------------
int main(void) {
  struct tms ts;
  clock_t clk, start;
  unsigned loop_len=6000000;
  unsigned loop;
  double dummy=0;

  start=times(&ts);
  loop=loop_len;
  while (loop--)
    dummy=dmin(5,1.0,2.0,3.0,dummy,(double)loop);
  clk=times(&ts)-start;
  cout << "    dmin: "
       << double(clk)/CLK_TCK << " seconds" << endl;

  start=times(&ts);
  loop=loop_len;
  while (loop--)
    dummy=min(1.0,2.0,3.0,dummy,loop);
  clk=times(&ts)-start;
  cout << "    overloaded min: "
       << double(clk)/CLK_TCK << " seconds" << endl;

  return 0;
}
//------------------------------------------------------
#include <stdarg.h>

double dmin(int nofargs, double d, ...)  {
  va_list ap; va_start(ap,d);

  while (--nofargs) {
    double v=va_arg(ap,double);
    if (v<d)
      d=v;
  }

  va_end(ap); return d;
}
//-----------------------------------------------------
//                     End of File
//-----------------------------------------------------

//------------------------------------------------------------------
// Opinions expressed here are my own only
//
// Constantine Antonovich,               Tel.  : ++972-8-942-3819
// software engineer,                    Fax   : ++972-8-942-3955
// Orbotech Ltd., P.O.Box 215,           Email: const@orbotech.co.il
// Yavne 81102, Israel
//------------------------------------------------------------------




  parent reply	other threads:[~1997-09-25  0:00 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-09-13  0:00 Fortran vs C++ vs. etc (has little to do with realtime anymore) Dr. Krishnan Chittur
1997-09-15  0:00 ` Area Industrial y Electromec�nica
1997-09-16  0:00 ` Vanesch P.
1997-09-16  0:00   ` Jeffrey Templon
1997-09-16  0:00     ` Gary L. Scott
1997-09-17  0:00       ` Jenn-Ching Luo
1997-09-17  0:00         ` Gary L. Scott
1997-09-16  0:00 ` Rick Hawkins
1997-09-18  0:00   ` Oleg Krivosheev
1997-09-19  0:00     ` Rick Hawkins
1997-09-23  0:00       ` Oleg Krivosheev
1997-09-23  0:00         ` Gary L. Scott
1997-09-24  0:00         ` Rick Hawkins
1997-09-24  0:00           ` Xingzeng Liu
1997-09-25  0:00         ` const [this message]
1997-09-25  0:00           ` Oleg Krivosheev
     [not found] <199709070005.TAA23336@manifold.algebra.com>
     [not found] ` <5utbth$rdi@snews2.zippo.com>
     [not found]   ` <34131554.73F2310E@roda.roc.servtech.com>
     [not found]     ` <34157696.16620299@nntp.interaccess.com>
     [not found]       ` <JTV2J.97Sep9170655@cobra.cs.virginia.edu>
     [not found]         ` <3415CE44.3BD531@calfp.co.uk>
     [not found]           ` <341644F2.763D@BZZvnet.ibm.com>
1997-09-12  0:00             ` Jeffrey Templon
1997-09-13  0:00               ` Joseph M. O'Leary
1997-09-16  0:00                 ` James F Cornwall
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox