factor-talk

[Prev] Thread [Next]  |  [Prev] Date [Next]

Re: [Factor-talk] "Interleaving" two arrays? Slava Pestov Fri Jun 19 04:00:27 2009

Your code still has the inefficiency of dynamic variable lookups on
every call to , as well as resizing the underlying array of the vector
being constructed. Here is a more direct version:

: intersperse ( seq1 seq2 -- seq )
    2dup [ length ] bi@ + <vector>
    [ '[ [ _ push ] bi@ ] 2each ] keep
    { } like ;

Slava

On Thu, Jun 18, 2009 at 3:56 AM, Phil Dawes<[EMAIL PROTECTED]> wrote:
> I tried:
> { 1 2 3 } { 3 2 1 } [ [ swap , , ] 2each ] { } make
>
> to remove the extra array creation and the concat pass. AFAICS it's
> quicker on large arrays:
>
> : testmake ( -- )
>   100000 >array dup reverse
>   [ [ swap , , ] 2each ] { } make drop ;
>
> : testconcat ( -- )
>   100000 >array dup reverse
>   [ 2array ] 2map concat drop ;
>
> ( scratchpad ) [ testconcat ] gc benchmark [ testmake ] gc benchmark

------------------------------------------------------------------------------
Crystal Reports - New Free Runtime and 30 Day Trial
Check out the new simplified licensing option that enables unlimited
royalty-free distribution of the report engine for externally facing 
server and web deployment.
http://p.sf.net/sfu/businessobjects
_______________________________________________
Factor-talk mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/factor-talk