Updating arrays without generating a new copy was introduce in J805 -- https://code.jsoftware.com/wiki/System/ReleaseNotes/J805
So in J701, that approach would indeed be slower (since it's creating a complete copy of the array). Also, virtual blocks (which speed up the }. approach) were introduced in J807. I guess I need to roll up my sleeves and do some benchmarking... Thanks, -- Raul On Fri, Aug 26, 2022 at 3:20 PM 'Mike Day' via General <[email protected]> wrote: > > These seem simpler and are possibly quicker, at least in J701 on this oldish > iPad: > 100 (1{I.@E.) A NB. Fails if there’s no (second) 100 in A > 719 > 100 ({.@}.@(I.@E.)) A NB. Returns 0 in that case > 719 > Easy to correct for such errors, of course. > > I tried > A =. ?1000000#1000 > find2 =: 13 : 'F + ((F=.>:y i. x) }. y) i. x' NB. No direct defs in J701 > ts'100 find2 A' > 0.020555 8.39091e6 > ts'100 (1{I.@E.) A' > 0.011503 76160 > ts'100 ({.@}.@(I.@E.)) A' > 0.007626 84864 > > Speed a bit better, space quite a lot, if happy with time & space tests. > > Only you know if it’s wise to overwrite the first occurrence of V in A! > > Cheers, > > Mike > > > > Sent from my iPad > > > On 26 Aug 2022, at 19:36, Raul Miller <[email protected]> wrote: > > > > i. returns the index of the first occurrence of a value within an array. > > > > So, when implementing an algorithm which needs the index of the second > > occurrence of the value within a (large) array, we need to do some > > additional work. > > > > let's say that our array is A, the value is V > > > > F=: A i. V NB. the index of the first occurrence of V > > > > What's the most efficient way of finding the second occurrence? > > > > One possibility is > > S=: (1+F) + ((1+F) }. A) i. V > > > > Another possibility, assuming that V is numeric and not zero, would be > > S=: (0 F} A) i. V > > > > But A is large, so perhaps a faster approach would be: > > S=: {{ while. V~:y{A do. y=. y+1 end. y }} F > > > > (Which has me wishing that S=: A i.!.F V would do the job, though I'm > > not sure that that's completely appropriate...) > > > > But, we can probably eliminate the need to generate a copy of A with a > > little extra work: > > > > A=: 0 F} A > > S=: A i. V > > A=: V F} A > > > > It seems to me that this is probably going to be the fastest approach. > > > > Can anyone think of a faster approach (or something with comparable > > speed which isn't quite so unwieldy?) > > > > Thanks, > > > > -- > > Raul > > ---------------------------------------------------------------------- > > For information about J forums see http://www.jsoftware.com/forums.htm > ---------------------------------------------------------------------- > For information about J forums see http://www.jsoftware.com/forums.htm ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
