This is the kind of solution I was looking for.
The approach of prefaced set of interest is
similar to character substitution algorithm.
(('csa',a.) {~ ('CSA',a.) i. ]) 'Char Subst Alg'
char subst alg
The similarity is the property of nub such
that only the first occurence of a given
item matters.
Another way to improve string performance, besides
indexing into B, is to use symbols or their numbers.
datai=: 3 : 0 NB. integer test data
N=: y
B=: 100+ i.N*10
A1=: 100+ ?.2 8$&(N&*)10
A2=: ,A1
i.0 0
)
dataz=: 3 : 0 NB. symbols
N=: y
B=: s:<@":"0] 100+ i.N*10
A1=: s:<@":"0] 100+ ?.2 8$&(N&*)10
A2=: ,A1
i.0 0
)
datan=: 3 : 0 NB. symbol numbers
N=: y
B=: 7 s:s:<@":"0] 100+ i.N*10
A1=: 7 s:s:<@":"0] 100+ ?.2 8$&(N&*)10
A2=: ,A1
i.0 0
)
Interestingly symbols in many cases give faster
result than their numbers.
datai 100
ts '((B e. ~.) #^:_1 #/.~)@/:~"1 A1'
0.0154313 1.07571e6
ts '<: #/.~"1 B,"1 A1'
0.0105399 4.19603e6
ts '((B e. ~.) #^:_1 #/.~)@/:~"1 A2'
0.00510707 1.06342e6
ts '<: #/.~"1 B,"1 A2'
0.00274979 1.0585e6
dataz 100
ts '((B e. ~.) #^:_1 #/.~)@/:~"1 A1'
0.0407538 1.07571e6
ts '<: #/.~"1 B,"1 A1'
0.0103865 4.19603e6
ts '((B e. ~.) #^:_1 #/.~)@/:~"1 A2'
0.0193156 3.14829e6
ts '<: #/.~"1 B,"1 A2'
0.00248104 1.0585e6
datan 100
ts '((B e. ~.) #^:_1 #/.~)@/:~"1 A1'
0.0671524 1.08186e6
ts '<: #/.~"1 B,"1 A1'
0.0290389 4.19603e6
ts '((B e. ~.) #^:_1 #/.~)@/:~"1 A2'
0.00833653 3.41043e6
ts '<: #/.~"1 B,"1 A2'
0.00434497 3.14739e6
--- Roger Hui <[EMAIL PROTECTED]> wrote:
> There is a faster, shorter, and easier to understand
> alternative:
> <: #/.~ B,A
> that is, preface with the universe of interest,
> calculate the counts, and decrement the counts due
> to the universe being tacked on.
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm