* Re: a**2 or a*a
1998-01-14 5:00 a**2 or a*a Yon Han CHONG
@ 1998-01-14 5:00 ` Phillip Helbig
1998-01-14 5:00 ` Arnaud Desitter
` (4 subsequent siblings)
5 siblings, 0 replies; 9+ messages in thread
From: Phillip Helbig @ 1998-01-14 5:00 UTC (permalink / raw)
In article <34BCC4A4.F27A00D1@cranfield.ac.uk>, Yon Han CHONG
<Y.H.Chong@cranfield.ac.uk> 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.
^ permalink raw reply [flat|nested] 9+ messages in thread* Re: a**2 or a*a
1998-01-14 5:00 a**2 or a*a Yon Han CHONG
1998-01-14 5:00 ` Phillip Helbig
@ 1998-01-14 5:00 ` Arnaud Desitter
1998-01-15 5:00 ` Robert Corbett
1998-01-14 5:00 ` Kevin G. Rhoads
` (3 subsequent siblings)
5 siblings, 1 reply; 9+ messages in thread
From: Arnaud Desitter @ 1998-01-14 5:00 UTC (permalink / raw)
To: Yon Han CHONG
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
_______________________________________________________________________
^ permalink raw reply [flat|nested] 9+ messages in thread* Re: a**2 or a*a
1998-01-14 5:00 ` Arnaud Desitter
@ 1998-01-15 5:00 ` Robert Corbett
0 siblings, 0 replies; 9+ messages in thread
From: Robert Corbett @ 1998-01-15 5:00 UTC (permalink / raw)
In article <34BCD934.41C6@bristol.ac.uk>,
Arnaud Desitter <Arnaud.Desitter@bristol.ac.uk> 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
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: a**2 or a*a
1998-01-14 5:00 a**2 or a*a Yon Han CHONG
1998-01-14 5:00 ` Phillip Helbig
1998-01-14 5:00 ` Arnaud Desitter
@ 1998-01-14 5:00 ` Kevin G. Rhoads
1998-01-14 5:00 ` Ed Overman
1998-01-14 5:00 ` Peter Shenkin
` (2 subsequent siblings)
5 siblings, 1 reply; 9+ messages in thread
From: Kevin G. Rhoads @ 1998-01-14 5:00 UTC (permalink / raw)
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
^ permalink raw reply [flat|nested] 9+ messages in thread* Re: a**2 or a*a
1998-01-14 5:00 ` Kevin G. Rhoads
@ 1998-01-14 5:00 ` Ed Overman
0 siblings, 0 replies; 9+ messages in thread
From: Ed Overman @ 1998-01-14 5:00 UTC (permalink / raw)
In article <01bd2110$37868cc0$LocalHost@stupidwin95>,
Kevin G. Rhoads <T_Rhoads@NoSpam.CLASSIC.MSN.COM> 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
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: a**2 or a*a
1998-01-14 5:00 a**2 or a*a Yon Han CHONG
` (2 preceding siblings ...)
1998-01-14 5:00 ` Kevin G. Rhoads
@ 1998-01-14 5:00 ` Peter Shenkin
1998-01-15 5:00 ` Steve Lionel
1998-01-15 5:00 ` Nick Wagg
5 siblings, 0 replies; 9+ messages in thread
From: Peter Shenkin @ 1998-01-14 5:00 UTC (permalink / raw)
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 *
^ permalink raw reply [flat|nested] 9+ messages in thread* Re: a**2 or a*a
1998-01-14 5:00 a**2 or a*a Yon Han CHONG
` (3 preceding siblings ...)
1998-01-14 5:00 ` Peter Shenkin
@ 1998-01-15 5:00 ` Steve Lionel
1998-01-15 5:00 ` Nick Wagg
5 siblings, 0 replies; 9+ messages in thread
From: Steve Lionel @ 1998-01-15 5:00 UTC (permalink / raw)
In article <34BCC4A4.F27A00D1@cranfield.ac.uk>, Yon Han CHONG
<Y.H.Chong@cranfield.ac.uk> 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
^ permalink raw reply [flat|nested] 9+ messages in thread* Re: a**2 or a*a
1998-01-14 5:00 a**2 or a*a Yon Han CHONG
` (4 preceding siblings ...)
1998-01-15 5:00 ` Steve Lionel
@ 1998-01-15 5:00 ` Nick Wagg
5 siblings, 0 replies; 9+ messages in thread
From: Nick Wagg @ 1998-01-15 5:00 UTC (permalink / raw)
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/
^ permalink raw reply [flat|nested] 9+ messages in thread