As a rule, code that avoids globals is better than code that uses globals. However, when I changed one of these functions to use an explicit argument, I was surprised by the performance difference:
amZ1y=: 3 : 0 zeros=. 0=y inf=. ($y)$_ zeros} y,: inf ) NB. versus the original amZ1=: 3 : 0 zeros=. M = 0 inf=. ($M) $ _ M=: zeros} M ,: inf ) M=: M2 NB. As in the preceding messages... ts 'amZ1 ''''' 0.077218335 4.4459898e8 ts 'amZ1y M' 0.27146536 4.1104422e8 NB. Big difference - but it's because of "cheating": ts 'amZ1 '''' [ M=: M2' 0.27324012 3.7749037e8 On Thu, Sep 18, 2008 at 2:08 PM, Sherlock, Ric <[EMAIL PROTECTED]>wrote: > ---Henry Rich wrote: > > The standard idiom for doing this is > > > > array =: (array = 0)} array ,: _ > > Thanks Henry, I'd remembered there was something like that and had been > searching for it unsuccessfully! Can you point me to a reference to note for > the future? > > > If array is big, you get a big boost from performing > > the update in place. To do that, you have to write > > > > zeros =. array = 0 > > inf =. ($array) $ _ > > array =: zeros} array ,: inf > > The performance boost doesn't seem to be that big in this case and comes at > the cost of space. > I'm not sure if there isn't a simpler way to compare the performance of > these different algorithms? > > M=: ((1024*1024),4)[EMAIL PROTECTED] > M2=: M > ts=: 6!:2 , 7!:[EMAIL PROTECTED] > > amZ0=: 3 : 'M=: (M=0)}M,:_' > > amZ1=: 3 : 0 > zeros=. M = 0 > inf=. ($M) $ _ > M=: zeros} M ,: inf > ) > > amZ2=: 3 : 0 > dat=. ,M > dat=. _ (I. dat=0)} dat > M=: ($M)$dat > ) > > amZ3=: 3 : 'M=: _&(([: I. 0 = ,@])}) M' > > amZ4=: 3 : 'M=: (+ _*0=]) M' > > ts 'amZ0''''' > 0.27791011148 310380800 > M=: M2 > ts 'amZ1''''' > 0.08244288031 444598976 > M=: M2 > ts 'amZ2''''' > 0.11220713806 134219840 > M=: M2 > ts 'amZ3''''' > 0.12162285989 71305856 > M=: M2 > ts 'amZ4''''' > 0.20750486445 134219904 > ---------------------------------------------------------------------- > For information about J forums see http://www.jsoftware.com/forums.htm > -- Devon McCormick, CFA ^me^ at acm. org is my preferred e-mail ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
