AFAIU that's the tricky thing with the definition of ts (R. Hui
mentioned this before):
first call:
ts '3 pow 100000'
3.3e_5 200768
second call:
ts '3 pow 100000'
1.7e_5 960
But (first call):
timer '3 pow 100000'
1.303445
second call:
timer '3 pow 100000'
3.8e_5
=@@i
R.E. Boss schreef:
With the help of M. pow even gets (much) faster.
R.E. Boss
-----Oorspronkelijk bericht-----
Van: [EMAIL PROTECTED] [mailto:programming-
[EMAIL PROTECTED] Namens Arie Groeneveld
Verzonden: zondag 20 juli 2008 18:35
Aan: Programming forum
Onderwerp: [Jprogramming] Speeding up dyad ^ for x: results
I don't know if this already passed the forum: how to speed up
exponentiation x^y with y>:0 for integer arguments giving a x: result;
e.g. if you have to change to x: in case:
y > ([:<.308*%@(10&^.))"0, x
([:<.308*%@(10&^.))"0, 2
1023
2^1023
8.988465674e307
2^1024
_
2x^1024
17976931348................
From existing algorithms:
pow=: 4 : 0"0
if. 0=y do. 1 return. end.
if. 2|y do. x * *: x pow -:<:y else. *: x pow -:y end.
)
or:
powi=: 4 : '(*:@]`(x**:@])@.[)/,1x,~2|<[EMAIL PROTECTED]:^:a:,y'"0
or:
powt=: ((*:@[$:-:@])`([**:@[$:-:@<:@])@.(2|]))`1:@.(0=])"0
and:
pow2=: (*/@:^2x^I.@|[EMAIL PROTECTED]:@])"0
>From ~help/dictionary/cwhile.htm :
exp =: 4 : 0"0
z=.1
a=.x
n=.y
while. n do.
if. 2|n do. z=.z*a end.
a=.*:a
n=.<.-:n
end.
z
)
ts '3x ^ 100000'
3.138055 362560
ts '3x pow 100000'
1.298968 166464
ts '3 powi 100000'
1.299312 171776
ts '3x powt 100000'
3.138818 209152
ts '3 pow2 100000'
3.716827 576896
ts '3x exp 100000'
3.154187 231680
pow and powi are reasonable candidates. Is there more to gain?
=@@i
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm