From: Oleg Krivosheev <kriol@fnal.gov>
To: 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: <vi8soutxaca.fsf@drabble.fnal.gov> (raw)
In-Reply-To: 342A1486.617C@orbotech.co.il
Hi,
Constantine Antonovich: <const@orbotech.co.il> writes:
>
> 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
gu ?
> 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).
well, FYI, being standard means only that - being standard.
Compiler is free to optimize or to insert 5min loop
into min/max function.
> In the contrary, in the example, we have overhead of the
> arguments number evaluation
sure...
if you know how do not evaluate number of arguments
for functions (not min/max in particular) with arbitrary
number of arguments, please share your knowledge with us.
> 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).
what do you mean "overhead"? Can you clarify ?
> 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;
> }
please, don't change the nature of the problem - Rick
was asking about min/max function with ARBITRARY
number of arguments.
> Such a way doesn't seem elegant enough and it only emulates
> arbitrary number
nope. it's not. If the problem is to provide function
which can handle arbitrary number of arguments,
it cannot be solved by providing function(s) which
can handle limited number of arguments. Try to reread
logic textbook next time.
> 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
well, you compare apples and oranges.
You provided solution for related but
another problem than Rick was asking for.
still, you know better way to handle
functions with arbitrary number of
arguments (using C++, templates, exceptions
whatever), please, share it with us.
OK
next prev 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 ` 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
1997-09-25 0:00 ` Oleg Krivosheev [this message]
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
[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