Operations with + tend to be very heavily optimized. GCD does not share
many of these optimizations (although +. on binary arguments, e.g. "or",
does). I'm not surprised by the behavior you've shown, although I
wouldn't necessarily be able to predict it. The operation (+./"1) could
be sped up like (+/"1) in J's implementation, but that requires
additional code and the operation is nowhere near common enough to
justify it.

The verb (+./&.|:) performs almost as quickly as the clunkier solution
you gave.

Marshall

On Sat, Mar 14, 2015 at 02:26:56PM +1300, Tikkanz wrote:
> 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
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to