From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,7a6a623afb38d7f7 X-Google-Attributes: gid103376,public X-Google-Thread: 1094ba,7a6a623afb38d7f7 X-Google-Attributes: gid1094ba,public X-Google-Thread: 109fba,94f5b26bc297a928 X-Google-Attributes: gid109fba,public From: Oleg Krivosheev Subject: Re: Fortran vs C++ vs. etc (has little to do with realtime anymore) Date: 1997/09/25 Message-ID: #1/1 X-Deja-AN: 275577001 Sender: kriol@drabble.fnal.gov References: <5ve7c6$f4m$1@info.uah.edu> <5vmbdl$v8f$1@news.iastate.edu> <5vu47d$ea8$1@news.iastate.edu> <342A1486.617C@orbotech.co.il> To: Constantine Antonovich: Organization: FERMILAB, Batavia, IL Newsgroups: comp.lang.c++,comp.lang.fortran,comp.lang.ada Date: 1997-09-25T00:00:00+00:00 List-Id: Hi, Constantine Antonovich: 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 > > #include > > #include > > > > 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 } > inline double min(double v1, double v2, double v3) { > double v=min(v1,v2); return v } > inline double min(double v1, double v2, double v3, double v4) { > double v=min(v1,v2,v3); return v } > inline double min(double v1, double v2, double v3, double v4, double v5) > { > double v=min(v1,v2,v3,v4); return v } 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