[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: integer exponentiation with negative exponent
- From: Peng Zhicheng <pengzhicheng1986@...>
- Date: Sat, 12 Apr 2014 13:35:55 +0800
On 04/12/2014 11:03 AM, Coda Highland wrote:
On Fri, Apr 11, 2014 at 8:01 PM, Coda Highland <chighland@gmail.com> wrote:
On Fri, Apr 11, 2014 at 7:23 PM, Peng Zhicheng
<pengzhicheng1986@gmail.com> wrote:
is the gain (if any) of a special integer pow operation worth the semantic
change?
It's probably faster, but the problem can be addressed by also using
the floating-point version when the RHS operand is negative. It seems
a simple enough change to me.
I also forgot: It preserves accuracy in the result for values that
need more than 52 but less than 65 bits to express. So it's more than
just performance; it's semantic correctness.
Of course, if the code doesn't handle overflow (I haven't looked) then
that's actually a weakness of using ints instead of floats.
/s/ Adam
I did some very simple experiments.
It seems the integer algorithm is faster in many (I'd say mostly normal) cases.
But the result is quite subtle.
Still, I don't think we need a integer version `pow'.
---------------8x-------------------------------------------------
pzc@pzc-laptop ~/w/l/src> uname -a
Linux pzc-laptop 3.11.10-7-desktop #1 SMP PREEMPT Mon Feb 3 09:41:24 UTC 2014 (750023e) i686 i686 i386 GNU/Linux
pzc@pzc-laptop ~/w/l/src> time ./lua -e 'local x, y = 9, 31; for i = 1, 100000000 do local _ = x ^ y end'
13.55user 0.00system 0:13.58elapsed 99%CPU (0avgtext+0avgdata 804maxresident)k
0inputs+0outputs (0major+251minor)pagefaults 0swaps
pzc@pzc-laptop ~/w/l/src> time ./lua -e 'local x, y = 9.0, 31; for i = 1, 100000000 do local _ = x ^ y end'
22.42user 0.00system 0:22.44elapsed 99%CPU (0avgtext+0avgdata 864maxresident)k
0inputs+0outputs (0major+266minor)pagefaults 0swaps
pzc@pzc-laptop ~/w/l/src> time ./lua -e 'local x, y = 9, 0x1001; for i = 1, 100000000 do local _ = x ^ y end'
23.45user 0.00system 0:23.48elapsed 99%CPU (0avgtext+0avgdata 804maxresident)k
0inputs+0outputs (0major+251minor)pagefaults 0swaps
pzc@pzc-laptop ~/w/l/src> time ./lua -e 'local x, y = 9.0, 0x1001; for i = 1, 100000000 do local _ = x ^ y end'
101.46user 0.01system 1:41.60elapsed 99%CPU (0avgtext+0avgdata 876maxresident)k
0inputs+0outputs (0major+269minor)pagefaults 0swaps
pzc@pzc-laptop ~/w/l/src> time ./lua -e 'local x, y = 9, 0x10001; for i = 1, 100000000 do local _ = x ^ y end'
28.67user 0.00system 0:28.70elapsed 99%CPU (0avgtext+0avgdata 804maxresident)k
0inputs+0outputs (0major+251minor)pagefaults 0swaps
pzc@pzc-laptop ~/w/l/src> time ./lua -e 'local x, y = 9.0, 0x10001; for i = 1, 100000000 do local _ = x ^ y end'
47.51user 0.00system 0:47.57elapsed 99%CPU (0avgtext+0avgdata 872maxresident)k
0inputs+0outputs (0major+268minor)pagefaults 0swaps
pzc@pzc-laptop ~/w/l/src> time ./lua -e 'local x, y = 9, 0xffff; for i = 1, 100000000 do local _ = x ^ y end'
32.49user 0.00system 0:32.53elapsed 99%CPU (0avgtext+0avgdata 804maxresident)k
0inputs+0outputs (0major+251minor)pagefaults 0swaps
pzc@pzc-laptop ~/w/l/src> time ./lua -e 'local x, y = 9.0, 0xffff; for i = 1, 100000000 do local _ = x ^ y end'
48.71user 0.00system 0:48.76elapsed 99%CPU (0avgtext+0avgdata 872maxresident)k
0inputs+0outputs (0major+269minor)pagefaults 0swaps
pzc@pzc-laptop ~/w/l/src> time ./lua -e 'local x, y = 9, 0xffffffffff; for i = 1, 100000000 do local _ = x ^ y end'
72.82user 0.01system 1:12.91elapsed 99%CPU (0avgtext+0avgdata 804maxresident)k
0inputs+0outputs (0major+251minor)pagefaults 0swaps
pzc@pzc-laptop ~/w/l/src> time ./lua -e 'local x, y = 9.0, 0xffffffffff; for i = 1, 100000000 do local _ = x ^ y end'
49.05user 0.00system 0:49.10elapsed 99%CPU (0avgtext+0avgdata 876maxresident)k
0inputs+0outputs (0major+269minor)pagefaults 0swaps
pzc@pzc-laptop ~/w/l/src> time ./lua -e 'local x, y = 9, 0x10000000001; for i = 1, 100000000 do local _ = x ^ y end'
61.18user 0.00system 1:01.26elapsed 99%CPU (0avgtext+0avgdata 804maxresident)k
0inputs+0outputs (0major+252minor)pagefaults 0swaps
pzc@pzc-laptop ~/w/l/src> time ./lua -e 'local x, y = 9.0, 0x10000000001; for i = 1, 100000000 do local _ = x ^ y end'
48.85user 0.00system 0:48.92elapsed 99%CPU (0avgtext+0avgdata 876maxresident)k
0inputs+0outputs (0major+269minor)pagefaults 0swaps
---------------8x-------------------------------------------------