From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fc89c,f8f184f3bc3517df,start X-Google-Attributes: gidfc89c,public X-Google-Thread: 1094ba,f8f184f3bc3517df,start X-Google-Attributes: gid1094ba,public X-Google-Thread: f678f,f8f184f3bc3517df,start X-Google-Attributes: gidf678f,public From: Yon Han CHONG Subject: a**2 or a*a Date: 1998/01/14 Message-ID: <34BCC4A4.F27A00D1@cranfield.ac.uk>#1/1 X-Deja-AN: 315854587 Content-Type: multipart/alternative; boundary="------------A8CC48D6092D0C18D5DB6DFF" Organization: Cranfield University Mime-Version: 1.0 Newsgroups: comp.lang.fortran,comp.unix.osf.misc,comp.unix.programmer --------------A8CC48D6092D0C18D5DB6DFF Content-Type: text/plain; charset=EUC-KR Content-Transfer-Encoding: 7bit Is there speed difference between a**2 and a*a. I am using DEC fortran 90. -- ---------------------------------------------------------------------- Yon Han Chong | Flow Control and Prediction Group | Tel: + 44 1234 750 111 ext.5420 Cranfield College of Aeronautics | E-mail: Y.H.Chong@cranfield.ac.uk Cranfield University | Fax: + 44 1234 752 149 United Kingdom | Mobile: + 44 958 381 395 ---------------------------------------------------------------------- --------------A8CC48D6092D0C18D5DB6DFF Content-Type: text/html; charset=EUC-KR Content-Transfer-Encoding: 7bit Is there speed difference between a**2 and a*a.

I am using DEC fortran 90.

-- 
----------------------------------------------------------------------
Yon Han Chong                     |
Flow Control and Prediction Group | Tel: + 44 1234 750 111 ext.5420
Cranfield College of Aeronautics  | E-mail: Y.H.Chong@cranfield.ac.uk
Cranfield University              | Fax: + 44 1234 752 149
United Kingdom                    | Mobile: + 44 958 381 395
----------------------------------------------------------------------
  --------------A8CC48D6092D0C18D5DB6DFF-- From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fc89c,f8f184f3bc3517df X-Google-Attributes: gidfc89c,public X-Google-Thread: 1094ba,f8f184f3bc3517df X-Google-Attributes: gid1094ba,public X-Google-Thread: f678f,f8f184f3bc3517df X-Google-Attributes: gidf678f,public From: helbig@multivac.jb.man.ac.uk (Phillip Helbig) Subject: Re: a**2 or a*a Date: 1998/01/14 Message-ID: <69iivp$47n$1@probity.mcc.ac.uk>#1/1 X-Deja-AN: 315864062 References: <34BCC4A4.F27A00D1@cranfield.ac.uk> Reply-To: helbig@man.ac.uk Organization: University of Manchester, NRAL Newsgroups: comp.lang.fortran,comp.unix.osf.misc,comp.unix.programmer In article <34BCC4A4.F27A00D1@cranfield.ac.uk>, Yon Han CHONG writes: > Is there speed difference between a**2 and a*a. There used to be, but any good compiler should do this sort of `optimisation' for you. In fact, I think it is probably better not to do any optimisation `by hand' since compilers these days should be smart enough to do it for you. The advantage of letting the compiler do it for you is that it will do what is best FOR IT, whereas if you rely on optimisation by hand, what is best might be different for different compilers. If you do the opposite of this sort of thing, writing more or less FORmula TRANslation, then the code will be easier to maintain since it is more obvious what you are doing, and why. And probably it will also be obvious to the compiler what you are doing, as well, which will let it make the best of it. > I am using DEC fortran 90. You'll probably get a response from Steve Lionel as well, but my guess is that DEC Fortran 90 will catch this at compile time and the compiled code will be the same in both cases (unless perhaps you have turned off all optimisation). Note that DEC Fortran 90 (and most? all? of their compilers) have a sensible optimisation by default, and if I recall correctly, don't ever do `dangerous' optimisation. -- Phillip Helbig Email .......... p.helbig@jb.man.ac.uk Nuffield Radio Astronomy Laboratories Tel. ..... +44 1477 571 321 (ext. 297) Jodrell Bank Fax ................. +44 1477 571 618 Macclesfield Telex ................. 36149 JODREL G UK-Cheshire SK11 9DL Web .... http://www.jb.man.ac.uk/~pjh/ My opinions are not necessarily those of NRAL or the University of Manchester. From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fc89c,f8f184f3bc3517df X-Google-Attributes: gidfc89c,public X-Google-Thread: f678f,f8f184f3bc3517df X-Google-Attributes: gidf678f,public X-Google-Thread: 1094ba,f8f184f3bc3517df X-Google-Attributes: gid1094ba,public From: Peter Shenkin Subject: Re: a**2 or a*a Date: 1998/01/14 Message-ID: <34BCD838.59E2@still3.chem.columbia.edu>#1/1 X-Deja-AN: 315872989 Content-Transfer-Encoding: 7bit References: <34BCC4A4.F27A00D1@cranfield.ac.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Organization: MacroModel Development Group, Columbia University Newsgroups: comp.lang.fortran,comp.unix.osf.misc,comp.unix.programmer Yon Han CHONG wrote: > > Is there speed difference between a**2 and a*a. Yes. a*a is faster to type. -P. -- ************ I have never seen even one episode of "Seinfeld." ************ * Peter S. Shenkin; Chemistry, Columbia U.; 3000 Broadway, Mail Code 3153 * ** NY, NY 10027; shenkin@columbia.edu; (212)854-5143; FAX: 678-9039 *** *MacroModel WWW page: http://www.columbia.edu/cu/chemistry/mmod/mmod.html * From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fc89c,f8f184f3bc3517df X-Google-Attributes: gidfc89c,public X-Google-Thread: f678f,f8f184f3bc3517df X-Google-Attributes: gidf678f,public X-Google-Thread: 1094ba,f8f184f3bc3517df X-Google-Attributes: gid1094ba,public From: Arnaud Desitter Subject: Re: a**2 or a*a Date: 1998/01/14 Message-ID: <34BCD934.41C6@bristol.ac.uk>#1/1 X-Deja-AN: 315877977 Content-Transfer-Encoding: 7bit Sender: usenet@fsa.bris.ac.uk (Usenet) X-Nntp-Posting-Host: gmb.ggy.bris.ac.uk References: <34BCC4A4.F27A00D1@cranfield.ac.uk> To: Yon Han CHONG Content-Type: text/plain; charset=us-ascii Organization: Department of Geography, University of Bristol, UK Mime-Version: 1.0 Newsgroups: comp.lang.fortran,comp.unix.osf.misc,comp.unix.programmer Yon Han CHONG wrote: > > Is there speed difference between a**2 and a*a. > No. A good compiler (and the digital's one is good in this respect) will use the best algorithm for a**b, i.e. a multiplication if b=2. Cheers, Arnaud -- _______________________________________________________________________ Arnaud Desitter Department of Geography, University of Bristol University Road, Bristol, BS8 1SS, United Kingdom Internet: Arnaud.Desitter@Bristol.ac.uk Fax: +44 (0)117 928 7878 _______________________________________________________________________ From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fc89c,f8f184f3bc3517df X-Google-Attributes: gidfc89c,public X-Google-Thread: f678f,f8f184f3bc3517df X-Google-Attributes: gidf678f,public X-Google-Thread: 1094ba,f8f184f3bc3517df X-Google-Attributes: gid1094ba,public From: "Kevin G. Rhoads" Subject: Re: a**2 or a*a Date: 1998/01/14 Message-ID: <01bd2110$37868cc0$LocalHost@stupidwin95>#1/1 X-Deja-AN: 315902059 References: <34BCC4A4.F27A00D1@cranfield.ac.uk> Newsgroups: comp.lang.fortran,comp.unix.osf.misc,comp.unix.programmer Before optimizing compilers and coprocessor hardware, a*a was normally recommend over a**2. However, that isn't necessarily true now. The best way to answer this question, write it both ways in test program(s) and time it. With compiler optimizations turned off you can get an accurate idea of what the underlying hardware likes. With optimizations turned on, your code has to be structured to avoid getting optimized out of existence. Fill a large array with values, and loop over that. On x86 platforms w/ 87 coprocessors the answer to the above tends to be different than w/o a coprocessor -- similarly, the answer for integer multiplication in the x86 vs. integers loaded into the 87 are often different. On other platforms, there will be similar variations. -- Kevin G. Rhoads, Ph.D. T_Rhoads@NO_SPAM.Classic.MSN.COM From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fc89c,f8f184f3bc3517df X-Google-Attributes: gidfc89c,public X-Google-Thread: f678f,f8f184f3bc3517df X-Google-Attributes: gidf678f,public X-Google-Thread: 1094ba,f8f184f3bc3517df X-Google-Attributes: gid1094ba,public From: overman@math.ohio-state.edu (Ed Overman) Subject: Re: a**2 or a*a Date: 1998/01/14 Message-ID: <69j7mn$av5$1@mathserv.mps.ohio-state.edu>#1/1 X-Deja-AN: 315950729 References: <34BCC4A4.F27A00D1@cranfield.ac.uk> <01bd2110$37868cc0$LocalHost@stupidwin95> Organization: Department of Mathematics, The Ohio State University Newsgroups: comp.lang.fortran,comp.unix.osf.misc,comp.unix.programmer In article <01bd2110$37868cc0$LocalHost@stupidwin95>, Kevin G. Rhoads wrote: >Before optimizing compilers and coprocessor hardware, a*a was normally >recommend over a**2. However, that isn't necessarily true now. > >The best way to answer this question, write it both ways in test program(s) >and time it. With compiler optimizations turned off you can get an >accurate idea of what the underlying hardware likes. With optimizations >turned on, your code has to be structured to avoid getting optimized out of >existence. Fill a large array with values, and loop over that. > >On x86 platforms w/ 87 coprocessors the answer to the above tends to be >different than w/o a coprocessor -- similarly, the answer for integer >multiplication in the x86 vs. integers loaded into the 87 are often >different. > >On other platforms, there will be similar variations. >-- >Kevin G. Rhoads, Ph.D. >T_Rhoads@NO_SPAM.Classic.MSN.COM Here are my timing results on a Sun ultrasparc 2/2200 in double precision. routine Mflops no opt optimized (-O) x*x scalar 6.629541 64.191444 x*x vector 4.424184 22.368158 x**2 scala 7.637088 16.617260 x**2 vecto 5.365106 23.890512 x**2. scal 7.654577 16.549056 x**2. vect 5.342769 23.796837 x**2.01 sc 1.897283 2.610437 x**2.01 ve 0.567783 0.654413 x*x*x scal 5.668141 32.354875 x*x*x vect 3.702529 21.510906 x**3 scala 4.628581 13.199857 x**3 vecto 3.874324 21.533822 x**3. scal 1.659949 2.512280 x**3. vect 0.569286 0.670852 x**3.01 sc 1.884676 2.692186 x**3.01 ve 0.571896 0.647438 The entire code is rather long, but each pair of subroutines (scalar and vector code) are similar to the following: subroutine pwr21(aa,nloop, $ times) ccccc ccccc a = a*a ccccc implicit double precision (a-h,o-z) times = cputim() do 10 i = 1,nloop aa = aa*aa 10 continue times = cputim() - times return end subroutine pwr22(a,b,ncycle,maxnpt, $ times) ccccc ccccc b(i) = a(i)*a(i) ccccc implicit double precision (a-h,o-z) dimension a(*),b(*) sum = 0. times = cputim() do 20 j = 1,ncycle do 10 i = 1,maxnpt b(i) = a(i)*a(i) 10 continue sum = sum + b(maxnpt) 20 continue times = cputim() - times b(maxnpt) = b(maxnpt) + sum return end (By the way, the reason for throwing in the variable sum is in case the compiler is smart enough to drop the do 20 loop since this simply repeats the do 10 loop ncycle times.) Ed Overman From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fc89c,f8f184f3bc3517df X-Google-Attributes: gidfc89c,public X-Google-Thread: f678f,f8f184f3bc3517df X-Google-Attributes: gidf678f,public X-Google-Thread: 1094ba,f8f184f3bc3517df X-Google-Attributes: gid1094ba,public From: Nick Wagg Subject: Re: a**2 or a*a Date: 1998/01/15 Message-ID: <34BDD22D.B43@lsl.co.uk>#1/1 X-Deja-AN: 316128258 Content-Transfer-Encoding: 7bit References: <34BCC4A4.F27A00D1@cranfield.ac.uk> Content-Type: text/plain; charset=us-ascii Organization: Laser-Scan Ltd. Mime-Version: 1.0 Newsgroups: comp.lang.fortran,comp.unix.osf.misc,comp.unix.programmer Yon Han CHONG wrote: > > Is there speed difference between a**2 and a*a. > The difference is unlikely to be considerable these days but experiment to find out. It is possible that the former case invokes a function to evaluate the expression and which would probably be slower - a quick examination of generated assembly code or of a compilation listing should indicate whether a function is called. -- Nick Wagg <>< (mailto:nickw@lsl.co.uk) Laser-Scan Ltd, Senior Software Engineer Science Park, Milton Rd, Tel: +44(0)1223 420414 (ext 213) Cambridge, CB4 4FY, UK. Fax: +44(0)1223 420044 http://www.lsl.co.uk/ From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fc89c,f8f184f3bc3517df X-Google-Attributes: gidfc89c,public X-Google-Thread: f678f,f8f184f3bc3517df X-Google-Attributes: gidf678f,public X-Google-Thread: 1094ba,f8f184f3bc3517df X-Google-Attributes: gid1094ba,public From: lionel@mail.dec.com (Steve Lionel) Subject: Re: a**2 or a*a Date: 1998/01/15 Message-ID: <69lds7$9nr@usenet.pa.dec.com>#1/1 X-Deja-AN: 316234710 Sender: lionel@quark (Steve Lionel) References: <34BCC4A4.F27A00D1@cranfield.ac.uk> Reply-To: lionel@mail.dec.com (Steve Lionel) Distribution: world Organization: Digital Equipment Corporation, Nashua NH Newsgroups: comp.lang.fortran,comp.unix.osf.misc,comp.unix.programmer In article <34BCC4A4.F27A00D1@cranfield.ac.uk>, Yon Han CHONG writes: |>Is there speed difference between a**2 and a*a. |> |>I am using DEC fortran 90. DIGITAL Fortran 90 generates identical code for these two constructs. My general advice is to use whichever form is more "obvious" to the reader given the computation you are performing. -- Steve Lionel mailto:Steve.Lionel@digital.com Fortran Development http://www.digital.com/info/slionel.html Digital Equipment Corporation 110 Spit Brook Road, ZKO2-3/N30 Nashua, NH 03062-2698 "Free advice is worth every cent" For information on DIGITAL Fortran, see http://www.digital.com/fortran From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fc89c,f8f184f3bc3517df X-Google-Attributes: gidfc89c,public X-Google-Thread: 1094ba,f8f184f3bc3517df X-Google-Attributes: gid1094ba,public X-Google-Thread: f678f,f8f184f3bc3517df X-Google-Attributes: gidf678f,public From: corbett@lupa.eng.sun.com (Robert Corbett) Subject: Re: a**2 or a*a Date: 1998/01/15 Message-ID: <69jotq$pv5@engnews1.Eng.Sun.COM>#1/1 X-Deja-AN: 317156833 References: <34BCC4A4.F27A00D1@cranfield.ac.uk> <34BCD934.41C6@bristol.ac.uk> Organization: Sun Microsystems Computer Corporation Newsgroups: comp.lang.fortran,comp.unix.osf.misc,comp.unix.programmer In article <34BCD934.41C6@bristol.ac.uk>, Arnaud Desitter wrote: >Yon Han CHONG wrote: >> >> Is there speed difference between a**2 and a*a. >> >No. A good compiler (and the digital's one is good in this respect) will >use the best algorithm for a**b, i.e. a multiplication if b=2. > >Cheers, >Arnaud While it is true that the best implementation of a**2 is a*a on most machines, there is no "best" algorithm for most of the remaining powers. Implementing a**3 as a*a*a causes the expression to have a 1 ulp error bound. Implementing it as pow(a, 3.0) will cause it to have an error bound of just over 1/2 ulp, assuming a good implementation of pow. The other integer powers that have obvious implementations are -1 and 1. The power zero has an obvious implementation that drives people who think we are doing math instead of computing nuts. Sincerely, Bob Corbett