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.
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

Reply via email to