[Jprogramming] Joining lists
Henry Rich
henryhrich at gmail.com
Mon Feb 1 02:23:15 UTC 2016
Right. It's something to keep in mind, methinks. If c is much larger
than a and b, the effect is bigger. And of course, you might be tempted
to write
a,b,c,d,e
when
; a;b;c;d;e
would be much better.
Have we talked about this question?
When does
; a;b;c
produce a different result from
a,b,c
(assuming here that a, b, c are not boxed)?
Henry Rich
On 1/31/2016 9:08 PM, Marshall Lochbaum wrote:
> It's not surprising at all if you know a little bit about how the verbs
> in question work. Dyad (,) creates a new array and copies its arguments
> into it. Thus computing (a,b,c) requires the steps:
> - Copy b and c into new array t1 (2e6 copy operations)
> - Copy a and t1 into new array t2 (3e6 copy operations).
>
> In contrast, dyad (;) is almost free, since it just creates an array of
> pointers to its arguments. Monad (;) computes the length of its result,
> allocates an array of that length, and then copies everything in, for a
> total of 3e6 copies. Provided we can't resize a to hold all the values
> (which would allow us to only copy b and c), this is optimal with J's
> array layout.
>
> The observed ratio of measurements for the two operations, in both space
> and time, is pretty close to (3%5) as we would expect from the above.
>
> Marshall
>
> On Sun, Jan 31, 2016 at 08:35:01PM -0500, Jose Mario Quintana wrote:
>> That is interesting, I was independently testing the same expression ;A ;
>> B ; C vs A , B , C with different sample nouns and it seems leaner and
>> meaner:
>>
>> st=. (, */&.:>@:(1 2&{))@:(] ; 7!:2@:] ; 6!:2)
>>
>> 'A B C'=. i.3 1000000
>>
>> 111 st&>'A , B , C' ; ';A ; B ; C'
>> ┌──────────┬────────┬─────────┬─────────┐
>> │A , B , C │50333184│0.0335228│1.68731e6│
>> ├──────────┼────────┼─────────┼─────────┤
>> │;A ; B ; C│33556608│0.0198092│664728 │
>> └──────────┴────────┴─────────┴─────────┘
>>
>> (A , B , C) -: i.3000000
>> 1
>> (;A ; B ; C) -: i.3000000
>> 1
>>
>> Using your sample nouns it is not as dominant but remains dominant
>> nevertheless:
>>
>> A=:?~1000000
>> B=:0.1+?~1000000
>> C=:?~1000000
>>
>> 111 st&>'A , B , C' ; ';A ; B ; C'
>> ┌──────────┬────────┬─────────┬─────────┐
>> │A , B , C │67110400│0.0496758│3.33377e6│
>> ├──────────┼────────┼─────────┼─────────┤
>> │;A ; B ; C│50333824│0.0360027│1.81216e6│
>> └──────────┴────────┴─────────┴─────────┘
>>
>> (A , B , C) -: (;A ; B ; C)
>> 1
>>
>>
>> JVERSION
>> Installer: j602a_win.exe
>> Engine: j803/2014-10-19-11:11:11
>> Library: 6.02.023
>>
>>
>>
>> On Sun, Jan 31, 2016 at 7:05 PM, Raul Miller <rauldmiller at gmail.com> wrote:
>>
>>> Oops, I meant:
>>>
>>> timespacex ';a;b;c'
>>> 0.012332 5.03338e7
>>> timespacex ';a;b;c'
>>> 0.011768 5.03338e7
>>> (;a;b;c)-:a,b,c
>>> 1
>>>
>>> Efficiency characteristics are the same, result (the most important
>>> part) is different.
>>>
>>> --
>>> Raul
>>>
>>> On Sun, Jan 31, 2016 at 7:04 PM, Raul Miller <rauldmiller at gmail.com>
>>> wrote:
>>>> If I define:
>>>> a=:?~1000000
>>>> b=:0.1+?~1000000
>>>> c=:?~1000000
>>>>
>>>> I get:
>>>> timespacex 'a,b,c'
>>>> 0.016585 6.71104e7
>>>> timespacex '>a;b;c'
>>>> 0.012863 5.0334e7
>>>> timespacex '>a;b;c'
>>>> 0.011867 5.0334e7
>>>> timespacex 'a,b,c'
>>>> 0.015703 6.71104e7
>>>>
>>>> So it looks like >a;b;c is slightly more efficient than a,b,c, but
>>>> it's nowhere close to a factor of 2, so I think I'd ignore this issue
>>>> in most contexts.
>>>>
>>>> --
>>>> Raul
>>>>
>>>>
>>>> On Sun, Jan 31, 2016 at 5:21 PM, Henry Rich <henryhrich at gmail.com>
>>> wrote:
>>>>> You have 3 large lists a, b, c (1000000 atoms each). You want to join
>>> them
>>>>> into one long list. What is the best way to do this?
>>>>>
>>>>> Henry Rich
>>>>> ----------------------------------------------------------------------
>>>>> 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
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
More information about the Programming
mailing list