Prev: a simple overflow example
Next: Overload assignment operator for an user defined type array with ?vector scripts
From: Dan Nagle on 10 Sep 2009 10:59 Hello, On 2009-09-10 06:51:38 -0400, "robin" <robin_v(a)bigpond.com> said: > "sumesh.pt" <sumesh.pt(a)gmail.com> wrote in message > news:52e82606-6d12-40b1-8b11-e7227294a922(a)a21g2000yqc.googlegroups.com... > | real*8 :: x > | integer :: i=10 > | x = x/i or x=x/dble(i) which is faster and accurate. > > There should be no difference whatsoever. > The conversion is from integer to double precision > in both cases. This assertion depends upon real*8 being double precision. On a 64-bit chip, it may not be. -- Cheers! Dan Nagle
From: Tim Prince on 10 Sep 2009 12:53 Dan Nagle wrote: > Hello, > > On 2009-09-10 06:51:39 -0400, "robin" <robin_v(a)bigpond.com> said: > >> There's no requirement to do any conversion at compile time. >> x/2 can be done at execution time on certain computers >> not by division but by scaling (and at considerable saving in time). > > Masking out the exponent, shifting, subtracting one, > and merging the new exponent back into the original number > may well take longer than one multiplication > on modern hardware. Not to mention checking for over/underflow. The compiler I learned on generated code for /2. which jumped over the subtraction from the exponent in the case of a 0. operand but didn't take care of all corner cases. > >> 2.0 can be treated as 2 for operations like x*2 and x/2, >> and those operations (* or div) are done at run time of course >> (the * being performed as x+x, again with considerable increase >> in speed). > > On modern hardware, multiply is often (at least almost) > as fast as addition. > Replacement of x*2 by x+x is most likely to improve performance where it balances the use of add and multiply arithmetic units. It's not necessarily possible to diagnose simply by looking at source code, even with knowledge of the target platform.
From: glen herrmannsfeldt on 10 Sep 2009 13:27 Dan Nagle <dannagle(a)verizon.net> wrote: (snip, robin wrote) <> There's no requirement to do any conversion at compile time. <> x/2 can be done at execution time on certain computers <> not by division but by scaling (and at considerable saving in time). < Masking out the exponent, shifting, subtracting one, < and merging the new exponent back into the original number < may well take longer than one multiplication < on modern hardware. S/360 and sucessors have a floating point HALVE instruction to divide by two. While there are no guarantees on the time, it should be faster than multiply or divide. <> 2.0 can be treated as 2 for operations like x*2 and x/2, <> and those operations (* or div) are done at run time of course <> (the * being performed as x+x, again with considerable increase <> in speed). < On modern hardware, multiply is often (at least almost) < as fast as addition. Especially on values with few one bits. Many compilers are good at finding the cases where constant division can be replaced by constant multiplication. I don't believe that an integer 2 would do better than a real 2.0, though. < In any case, run your own tests on your own hardware < before coming to a conclusion. Once you decide that the time matters that much. -- glen
From: robin on 11 Sep 2009 21:19 "Dan Nagle" <dannagle(a)verizon.net> wrote in message news:h8b491$i8m$2(a)news.eternal-september.org... | Hello, | | On 2009-09-10 06:51:38 -0400, "robin" <robin_v(a)bigpond.com> said: | | > "sumesh.pt" <sumesh.pt(a)gmail.com> wrote in message | > news:52e82606-6d12-40b1-8b11-e7227294a922(a)a21g2000yqc.googlegroups.com... | | > | real*8 :: x | > | integer :: i=10 | > | x = x/i or x=x/dble(i) which is faster and accurate. | > | > There should be no difference whatsoever. | > The conversion is from integer to double precision | > in both cases. | | This assertion depends upon real*8 being double precision. On the assumption that real*8 actually means anything, which it doesn't on some compilers. The OP is advised to stick to standard constructs.
From: robin on 11 Sep 2009 21:27
"Dan Nagle" <dannagle(a)verizon.net> wrote in message news:h8b461$i8m$1(a)news.eternal-september.org... | Hello, | | On 2009-09-10 06:51:39 -0400, "robin" <robin_v(a)bigpond.com> said: | | > There's no requirement to do any conversion at compile time. | > x/2 can be done at execution time on certain computers | > not by division but by scaling (and at considerable saving in time). | | Masking out the exponent, shifting, subtracting one, | and merging the new exponent back into the original number | may well take longer than one multiplication | on modern hardware. Multipliocation? whio said anything about multiplication? The operation is DIVISION here. It may interest you to know that those operations take place for any arithmeric operation , whether it be +, -, * and divide. With halving, division reqires no actual division so that is saved. That is why the operation is about 10 times fasyter than actual division. And IF the operation were multiplication by 2, why that can be achieved by simple addtion. Again, faster than multiplication. | > 2.0 can be treated as 2 for operations like x*2 and x/2, | > and those operations (* or div) are done at run time of course | > (the * being performed as x+x, again with considerable increase | > in speed). | | On modern hardware, multiply is often (at least almost) | as fast as addition. Often not. |