OK, thank you. I had not realized that J did the linear scan backwards.
I guess I saw that in some examples such as alternating sum -/ and
continued fraction (+%)/ but plain forgot.  Here is an experiment again.

slash=: 1 : 0 NB.  For associative binary operation u.
N=.# y
if. 1=N do. y return. end.
y=.|.y
z=.((N-1){y) u ((N-2){y)
for_i. |.i.(N-2)  do. z=. z u (i{y) end.
z
)

stringComma=: 4 : 0
(":x),',',(":y)
)

slash1=: 1 : 0
N=.#y
if. 1=N do. y return. end.
U=.u~
z=.((N-1){y) U ((N-2){y)
for_i. |.i.(N-2) do. z=.z U (i{y) end.
z
)

timex'stringComma / i.1e5'
timex'stringComma slash1 i.1e5'
timex'stringComma slash i.1e5'

NB.   timex'stringComma / i.1e5'
NB. 17.6212
NB.   timex'stringComma slash1 i.1e5'
NB. 9.12946
NB.   timex'stringComma slash i.1e5'
NB. 4.43109
NB. J902, Windows 10.

It still does not do 1e6, but the modified for loop scans seem
to beat / on stringComma.

Is there a real good reason for doing the linear scan backwards,
the alternating sum and continued fractions notwithstanding?

In lisp, if they need to cons up a long list, they do it so that the
front grows and combine consing with reversals; it is usually faster
than trying to grow a list at the end.

Yours sincerely,
Imre Patyi



On Sat, Jun 12, 2021 at 2:19 PM Henry Rich <[email protected]> wrote:

> Appending to the end of a string can be done in place.  Adding on to the
> beginning cannot.
>
> If you repeat
>
> stg =: stg , new
>
> it will be fast.
>
> (":x) might return the same string as x (it ought to) but that string
> cannot be inplaced because it is still used for x.
>
>
> Henry Rich
>
> On 6/12/2021 2:12 PM, Imre Patyi wrote:
> > NB. Dear Programming in J,
> > NB.
> > NB. I have a question about memory management, but I cannot
> > NB. formulate it properly, but here is another question with
> > NB. examples that seems to pose a question about memory management.
> > NB.
> > NB. I have a question about ": applied to a string.
> > NB. It seems that the command
> > NB. ":s
> > NB. allocates a new string even when s is already
> > NB. a string.  Is that the intended behavior?
> > NB.
> > NB. Example task:  Form the string 0,1,2,...,999999 , i.e.,
> > NB. put commas between the first million numbers.
> > NB.
> > NB. Example code:
> >
> > f1=:3 : '(4 : ''(":x),'''','''',(":y)'')/ i.y'
> > f1 5
> > timex'f1 1e5'  NB.  It is way too slow to do timex'f1 1e6' .
> >
> > f2=:3 : 0
> > n=.y
> > s=.''
> > for_i. i.n do. s=.s,(":i),',' end.
> > }:s
> > )
> > f2 5
> > timex'f2 1e6'
> >
> >
> > f3=:3 : 0
> > n=.y
> > s=.''
> > for_i. i.n do. s=.(":s),(":i),',' end.
> > }:s
> > )
> > f3 5
> > timex'f3 1e5' NB. It is way to slow to do timex'f3 1e6' .
> >
> > f4=:3 : ''',''(I.'' ''=a)}a=.":i.y'
> > f4 5
> > timex'f4 1e6'
> > NB.
> > NB. Initially, I thought that f1 would be fast enough, but
> > NB. it is in fact slow.  I was surprised that the for loop
> > NB. f2 was fast enough; the for loop f3 is slow again with
> > NB. the unnecessary ":s or (toString s) instead of s.  I
> > NB. think it breaks some canned combinations of J.  I guess
> > NB. f4 is roughly as fast as it gets without thinking.
> >
> > toString=:3 : '(":`]@.(2=3!:0 y))y'
> > toString1=:3 : 0
> > if.2=3!:0 y do. y return. else. ":y return. end.
> > )
> > f5=:3 : '(4 : ''(toString x),'''','''',(toString y)'')/ i.y'
> > f5 5
> > timex'f5 1e5'
> > NB.
> > NB. Not allocating a new string (what I mean is using
> > NB. toString instead of ":) does not seem to help the f5
> > NB. version of f1 in J902 but it does in J805.  There may
> > NB. be some other memory management issue there as well.  I
> > NB. think the handling of f1 turns a linear problem into a
> > NB. quadratic one.
> >
> > f6=:3 :'}:;(<@(":,]&'',''))"0]i.y'
> > f6 5
> > timex'f6 1e6'
> >
> > NB. The form f6 tells us that it is not the individual
> > NB. commands ":0, ":1, ..., ":999999 that are slow, but
> > NB. something inside the handling of f1 is.
> > NB.
> > NB. The whole issue reminds me of the ancient Java bug when
> > NB. you print a long string by concatenating small strings
> > NB. to the end.  Does J do any doubling of arrays in the
> > NB. memory allocator?  I am not sure how the scan ()/ in f1
> > NB. should work, but I thought it would be similar to a for
> > NB. loop such as f2 or f3.
> >
> > NB. To react to an answer to an earlier question of mine,
> > NB. thanks, I enjoy my new big fat cursor in J902.
> > NB. However, I did not get any response about fixing the
> > NB. "run clipboard F8" command in J902 (which does not put
> > NB. the cursor on a fresh line after an F8 paste as it used
> > NB. to do and should still).
> > NB.
> > NB. Thank you very much!
> > NB.
> > NB. Yours sincerely,
> > NB. Imre Patyi
> > NB.
> > NB. J902, Windows 10.
> >
> > ----------------------------------------------------------------------
> > For information about J forums see http://www.jsoftware.com/forums.htm
>
>
>
> --
> This email has been checked for viruses by AVG.
> https://www.avg.com
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to