[Jprogramming] Speed of prefix and suffix
Ben Gorte - CITG
B.G.H.Gorte at tudelft.nl
Mon Feb 1 10:51:26 UTC 2016
Good morning,
I stumbled into something I found funny, but perhaps is widely known: Sometimes, u/\. is much much faster than u/\ . In my application it makes the difference between 'perfect' and 'useless'. For example:
timespacex 'plus /\ i.1000'
0.209933 44928
timespacex 'plus /\. i.1000'
0.000542 19840
Apparently J realizes that u/\. can be done incrementally (going from back to front) for any u, whereas the u/ part of u/\ is done from the beginning over and over. With a vplus that says what it is doing you see:
vplus/ \ i.5
0+1
1+2
0+3
2+3
1+5
0+6
3+4
2+7
1+9
0+10
0 1 3 6 10
vplus/ \. i.5
3+4
2+7
1+9
0+10
10 10 9 7 4
Indeed, appendix B of the dictionary contains the line
u/\. monad special code
whereas there is no such line for u/\ .
In addition there is special code for many u/\ with built-in u. For user defined (associative, commutative?) u you can use u/\. &. |. instead of u/\ and it will be much faster.
timespacex 'plus /\.&.|. i.1000'
0.000571 28928
vplus /\. &.|.i.5
1+0
2+1
3+3
4+6
0 1 3 6 10
Ben
More information about the Programming
mailing list