I was searching integer solutions (k,m) for
   1^2 + 2^2 + ... + k^2 = m^2
and I found that one expression runs much faster than the other:

   (#~(0=1|[:%:+/@:([:*:1+i.))"0) 2+i.9999
24
   (#~(0=1|[:%:+/@:(*:&>:&i.))"0) 2+i.9999
24

The first one is ~100 times faster, and moreover, the expression with '+/'
is faster, then without it!

   (3) 6!:2 '(+/@:([:*:1+i.))"0 (i.9999)'         NB. with +/ and fork
0.143744
   (3) 6!:2 '(+/@:(*:&>:&i.))"0 (i.9999)'        NB. with +/ and train
13.4614

   (3) 6!:2 '([:*:1+i.)"0 (i.9999)'      NB. without +/
0.608895
   (3) 6!:2 '(*:&>:&i.)"0 (i.9999)'
14.0192

As for '+/' I think the explanation is that no additional arrays are
created, the sums are just computed on the fly.
But the question remains, why the (equivalent) fork is so much faster than
the train?

Hm, changing the long train to a shorter one with a fork helps:
   (3) 6!:2 '(*:&(1+i.))"0 (i.9999)'
0.62027
So, what's the rule?

---
Georgiy Pruss
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to