How about this? fdeb2=: 13 :'y#~-.('' ''=y)*.1|.'' ''=y' fdeb2 ] #~ [: -. (' ' = ]) *. 1 |. ' ' = ]
Linda -----Original Message----- From: programming-boun...@jsoftware.com [mailto:programming-boun...@jsoftware.com] On Behalf Of Marshall Lochbaum Sent: Wednesday, January 04, 2012 12:42 PM To: Programming forum Subject: Re: [Jprogramming] Faster deb If you really want top-of-the-line performance, you'll have to go to a sequential machine. Here's my solution, which leaves the leading and trailing blanks in, then specifically deletes them. I don't claim that these tests are universal; it's quite plausible that a different mixture of blanks and words would give different results. Of course, benefits are marginal, and if performance is REALLY critical, I would advise just wasting the 20 lines and 10 minutes that it takes to code the thing in C. ws =. ' ',TAB,LF dlb =. }.^:(ws e.~{.) NB. delete leading blank dtb =. }:^:(ws e.~{:) NB. delete trailing blank m =. ws -.@:e.~ a. s =. 2 2 2$ 0 1 1 2 0 2 1 0 sdeb =: [: dlb@:dtb (1;s;m)&;: fdeb =: [: dlb [ #~ [: -. [: (] *. 1&|.) ' ' = [ test =. ; ' test'&,&.> (1e5?@$10) <@$"0 ]' ' 100 (6!:2) 'deb test' 0.0167294 100 (6!:2) 'sdeb test' 0.012571 100 (6!:2) 'fdeb test' 0.0162943 (deb -: sdeb) test 1 (deb -: fdeb) test 1 Marshall On Wed, Jan 4, 2012 at 11:53 AM, Ian Clark <earthspo...@gmail.com> wrote: > okay, I didn't read Raul's posting properly. > > On Wed, Jan 4, 2012 at 4:44 PM, Ian Clark <earthspo...@gmail.com> wrote: > > Raul's points are valid, but as a technique I like it. I'm going to > > add it to my collection of idioms for a monograph on string-handling. > > > > Rotate-and-compare can be generalized to detect any given letter > > followed/preceded by some other. But in the speed stakes, there's > > often extra housekeeping handling the string ends. > > > > Harking back to a recent thread where (;:inv) came up, how about these > too? > > > > wdeb=: ;:inv @ ;: > > udeb=: ] &. ;: NB. a faster variant of the same idea > > > > Not as fast as either deb or fdeb, (besides mishandling punctuation) > > but with a certain idiot appeal. And try replacing (]) with (|.), > > (~.), sort, etc. > > > > In my monograph, (;:) will have a chapter to itself. > > > > > > On Wed, Jan 4, 2012 at 1:54 PM, Raul Miller <rauldmil...@gmail.com> > wrote: > >> As you point out, these are not equivalent. > >> > >> # TEST=: ' this is a test ' > >> 26 > >> # deb TEST > >> 14 > >> # fdeb TEST > >> 15 > >> > >> Note also: > >> # ]&.;: TEST > >> 14 > >> > >> That said: > >> # ]&.;: ' Don''t do this... ' > >> |open quote > >> > >> Also, the timing difference between 3.446e_6 and 3.293e_6 is > inconsequential. > >> > >> -- > >> Raul > >> > >> On Wed, Jan 4, 2012 at 2:14 AM, Alan Stebbens <a...@stebbens.org> wrote: > >>> "delete extra blanks" from strings.ijs has this definition: > >>> > >>> deb =: #~ (+. (1: |. (> </\)))@(' '&~:) > >>> > >>> NB. Here is a slightly faster deb: > >>> > >>> fdeb =: [ #~ [: -. [: (] *. 1&|.) ' ' = [ > >>> > >>> NB. s is the test string; w is its boxed words > >>> > >>> w=.;:s=.'now is the time for all good men to come to the aid of > their country' > >>> w > >>> +---+--+---+----+---+---+----+---+--+----+--+---+---+--+-----+-------+ > >>> |now|is|the|time|for|all|good|men|to|come|to|the|aid|of|their|country| > >>> +---+--+---+----+---+---+----+---+--+----+--+---+---+--+-----+-------+ > >>> > >>> NB. show > >>> 1000 tx 'deb ,>w' > >>> 3.446e_6 3072 > >>> > >>> 1000 tx 'fdeb ,>w' > >>> 3.293e_6 3072 > >>> > >>> NB. same value for both the old and new fun > >>> (deb -: fdeb),>w > >>> 1 > >>> > >>> NB. the results match the original string > >>> s -: fdeb ,>w > >>> 1 > >>> > >>> However, it's not exactly the same as deb: it doesn't remove a single > leading blank, as does deb (which, IMHO, is not necessarily an "extra" > blank anyway). > >>> > >>> Alan > >>> > >>> > >>> > >>> ---------------------------------------------------------------------- > >>> 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 ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm