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,c615e41a65104004 X-Google-Attributes: gid103376,public From: pfk@schnecke.offl.uni-jena.de (Frank Klemm) Subject: Re: Performance Ada and C Date: 1998/07/07 Message-ID: #1/1 X-Deja-AN: 369373919 X-Nntp-Posting-Host: schnecke.offl.uni-jena.de Sender: news@fsuj50.rz.uni-jena.de (News System) References: <35921271.E51E36DF@aonix.fr> <3598358A.73FF35CC@pipeline.com> <6nh762$66i@netline.jpl.nasa.gov> <359CB19D.EDAD6D1F@cl.cam.ac.uk> <359D4C6F.5691A370@cl.cam.ac.uk> Organization: Pbzchgre fvaq hasruyone, Qnir Newsgroups: comp.lang.ada Date: 1998-07-07T00:00:00+00:00 List-Id: On Fri, 03 Jul 1998 21:26:07 +0000, Markus Kuhn wrote: > >Basically the only difference is that I replaced the macros in the C >version by inline functions. Are as far as performance and optimization >are concerned, I just think of inlined functions as a sort of macros >with type checking, so this should not cause the difference. > Sorry, Markus, but inline functions and macros are very different. Generally speaking, macros are more dangerous but faster. We have a function returning x or y whether a is equal to b or not. int inline fn1(int a, int b, long x, long y) { return a==b ? x : y; } and a macro #define fn2(a,b,c,d) ((a)==(b) ? (x) : (y)) /* brackets, it's only a text substitution */ A function call of fn1 generates the following code: c = fn1(expr1,expr2,expr3,expr4); calc expr4 and store it to a register d1 -- 1 reg in use calc expr3 and store it to a register d2 -- 2 regs in use calc expr2 and store it to a register d3 -- 3 regs in use calc expr1 and comprare it with d3 if equal goto lbl1 store d1 to the accu goto lbl2 lbl1: store d2 to the accu lbl2: store accu to c A macro call of fn2 generates the following code: calc expr2 and store it to a register d3 -- 1 reg in use calc expr1 and comprare it with d3 if equal goto lbl1 calc expr4 and store it to the accu goto lbl2 lbl1: calc expr3 and store it to the accu lbl2: store accu to c Problems of the inline version: * early evaluation of expression => needs more registers or a very heavy optimizer * evaluation of unused parameters (this is guaranteed !!!) Tested with several C/C++-Compilers (Turbo-C++, ZTC++, WC++). Another impressive example is: unsigned inline sl2(int x, unsigned t1, unsigned t2) { return x ? t1 : t2; } #define sl1(x,t1,t2) ((x)?(t1):(t2)) int main(void) { printf("Calling fn1... \n"); printf("%u\n",sl1(1,sleep(1),sleep(100))); printf("Calling fn2... \n"); printf("%u\n",sl2(1,sleep(1),sleep(100))); return 0; } -- Frank Klemm /------\ /-----------------------------------------------------\ | eMail: || pfk@uni-jena.de | home: pfk@schnecke.offl.uni-jena.de | | Tel: || | home: +49 (3641) 390545 | | sMail: || Frank Klemm, Ziegesarstr. 1, D-07747 Jena, Germany | \------/ \-----------------------------------------------------/