---Brian Schott wrote:
>         I have been studying the variations on normalrand
> some more and have a few improvements and some puzzlement
> about J's behavior, too.
[snip]
>         The one defined as normalrand2 is most like the
> original BM algorithm except that the output of normalrand2
> follows the second pair pattern which is not exactly how BM
> is most often described. The alternating ababab... pattern
> could be produced with the commented out line in
> normalrand2, but is not needed to get good variates. From my
> comparisons using ts, normalrand2 is the overall best
> performer, requiring about 2/3 of the time and 1/2 of the
> space as does the current normalrand in statdist.ijs . Even
> the current one, normalrand, does a quick job with modest
> use of space, btw.

Thanks for this Brian, it just goes to show that sometimes keeping things 
simple and explicit is sweet!

>         It is surprising to me that normalrand3 and
> normalrand3a are not almost identical in performance and
> that 3a requires more than twice the processing time and
> almost twice the space that 3 takes, when the main
> difference between the two is the fork 2&o. ,. 1&o. versus 2
> 1&o. .
>
>         It is also surprising to me that normalrand4 is 7
> times slower than normalrand, (while it did gain on space
> considerably). I was able to speed it up a little with the
> added parentheses in normalrand5, but lost much more
> relatively in space. I cannot be sure whether the slowness
> is due more to the tacit aspect of 4 and 5 or in the
> currying of the trig functions in conjunction with the
> rand01 arguments. I would have been very pleased if these
> two tacit definitions were better performers because they
> are so concise in J.

I think versions 4 and 5 are a good example of why Henry suggests to use @: 
rather than @ by default in J4C Programmers.  They (especially normalrand5) are 
so slow because the use of @ causes J to "loop through" many of the arrays item 
by item rather than processing them as a whole. See normalrand4a for comparison:

normalrand4a=: ({.,)(2 1&o."1 0@: +:@:o.@:rand01  * 
[:%:-@:+:@:^.@:rand01)@>[EMAIL PROTECTED]:

By making sure that you iterate over the short left array rather than the long 
right array also makes a big difference see normalrand6a:

normalrand6a=: ({.,)((2 1 o."0 1 +:@:o.@:rand01) *"1 [: %: 
-@:+:@:^.@:rand01)@>[EMAIL PROTECTED]:

Tacit normalrand6a is now leaner and faster than all the other candidates

Relative to the current normalrand:
 * normalrand2  is 1.386 times faster and 2.499 times leaner
 * normalrand6a is 1.548 times faster and 3.999 times leaner

However I'm pleased to see that the simple, easy to read normalrand2 is so 
competitive. Replacing - +: with _2* would make it slightly faster.

    numb=: 100000
    10 ts 'normalrand   numb'
0.024695086945 10488320
    10 ts 'normalrand2  numb'
0.017816286707 4196608
    10 ts 'normalrand3  numb'
0.019537224703 8392256
    10 ts 'normalrand3a numb'
0.042003767239 12586688
    10 ts 'normalrand4  numb'
0.11628898366 2622592
    10 ts 'normalrand4a numb'
0.041251541746 7341312
    10 ts 'normalrand5  numb'
0.082978884188 6292736
    10 ts 'normalrand6a numb'
0.016935811674 2622592



>         Btw, if I leave out the rank "1 0 in either
> normalrand4 or normalrand5, I crash J when I do ts in the
> way I did these tests.
>
> NB. ***************************************
>
> rand01 =: [EMAIL PROTECTED] 0:
>
> normalrand=: 3 : 0
> (2 o. +: o. rand01 y) * %: - +: ^. rand01 y
> )
>
> normalrand2=: 3 : 0
> n =. >. -: y
> a=. %: - +: ^. rand01 n
> b=.      +: o. rand01 n
> r1 =. a * 2 o. b
> r2 =. a * 1 o. b
> NB. y{.,r1,.r2        NB. not required
> y{.r1,r2
> )
>
> normalrand3=: 3 : 0
> n =. >. -: y
> y{.,((2&o.,.1&o.) +: o. rand01 n) *"1 0 %: - +: ^. rand01 n
> )
>
> normalrand3a=: 3 : 0
> n =. >. -: y
> y{.,((2 1&o.)"1 0 +: o. rand01 n) *"1 0 %: - +: ^. rand01 n
> )
>
> normalrand4=: ({.,)(2 1&o."1 0@ +:@[EMAIL PROTECTED]  *
> [:%:[EMAIL PROTECTED]:@[EMAIL PROTECTED])@>[EMAIL PROTECTED]:
> normalrand5=: ({.,)(2 1&o."1 0@(+:@[EMAIL PROTECTED]) *
> [:%:[EMAIL PROTECTED]:@[EMAIL PROTECTED])@>[EMAIL PROTECTED]:
>
> NB. ***************************************
>
>    ts 'normalrand 1000'
> 0.00134699 84480
>    ts 'normalrand2 1000'
> 0.00091691 35072
>    ts 'normalrand3 1000'
> 0.00100492 69184
>    ts 'normalrand3a 1000'
> 0.00270991 102080
>    ts 'normalrand4 1000'
> 0.00959293 21632
>    ts 'normalrand5 1000'
> 0.00741104 50432
>
[snip]
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to