Why isn't the final line 3 12 21 30 39
(i.5){TST=: i. 2e6 3
0 1 2
3 4 5
6 7 8
9 10 11
12 13 14
$TST
2000000 3
(i.5){(0&{"1 + 1&{"1 + 2&{"1) TST
3 12 21 30 39
(i.5){+/"1 TST
3 12 21 30 39
5{.TST
0 1 2
3 4 5
6 7 8
9 10 11
12 13 14
+/"1 5{.TST
3
Linda
-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Tikkanz
Sent: Friday, March 13, 2015 9:27 PM
To: Programming JForum
Subject: [Jprogramming] Performance query
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>
http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm