I noticed some interesting behaviour when trying to simplify a J sentence.
The simple J-like solution was slower than the more complex proscriptive
one.

Given an array:
   TST=: i. 2e6 3

To sum the columns with each other (i.e. sum the rows), I can
  a) extract each column and the lists together:

   (0&{"1 + 1&{"1 + 2&{"1) TST

  b) sum the items in each row

   +/"1 TST

Sentences a) and b) are equivalent:
   (+/"1 -: (0&{"1 + 1&{"1 + 2&{"1)) TST
1

And as I expected, the simpler sentence is faster and leaner.
   10 timespacex '(0&{"1 + 1&{"1 + 2&{"1) TST'
0.0439944 5.03372e7
   10 timespacex '+/"1 TST'
0.0138071 1.67798e7

If I use GCD instead of Plus, the approaches are still equivalent, but the
simpler sentence is much slower (while still being leaner).
   (+./"1 -: 0&{"1 +. 1&{"1 +. 2&{"1) TST
1
   10 timespacex '(0&{"1 +. 1&{"1 +. 2&{"1) TST'
0.132607 5.03372e7
   10 timespacex '+./"1 TST'
0.943093 1.67805e7

Is this to be expected because of differences in the nature of the GCD and
Plus operations, or does it reflect an opportunity for future optimisation
of ( +. ) ?
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to