On Dec 23, 2:31 am, rvtol+use...@isolution.nl (Dr.Ruud) wrote:
> sftriman wrote:
> > 1ST PLACE - THE WINNER:  5.0s average on 5 runs
>
> > # Limitation - pointer
> > sub fixsp5 {
> > ${$_[0]}=~tr/ \t\n\r\f/ /s;
> > ${$_[0]}=~s/\A //;
> > ${$_[0]}=~s/ \z//;
> > }
>
> Just decide to change in-place, based on the defined-ness of wantarray.
>
> sub trim {
>      no warnings 'uninitialized';
>
>      if ( defined wantarray ) {
>          # need to return scalar / list
>          my @values= @_;
>          s#^\s+##s, s#\s+$##s foreach @values;
>          return wantarray ? @values : $values[0];
>      }
>
>      # need to change in-place
>      s#^\s+##s, s#\s+$##s foreach @_;
>      return;
>
> }    #trim
>
> --
> Ruud

Hi there,

You're missing the tr to squash space down, but I see what you're
doing.
I never need to "trim" an array at this point, but if I did...

So I think it can boil down to:

sub fixsp7 {
s#\A\s+##, s#\s+\z##, tr/ \t\n\r\f/ /s foreach @_;
return;
}

This is in keeping consistent with my other 6 test cases.  I run it
against
several test strings including some with line breaks to make sure the
results are always the same.  Note I am using \A and \z and not ^ and
$.
Still, I think this has the flavor of what you intended.

Result: 5 trial runs over the same data set, 1,000,000 times, average
time was 16.30s.  All things considered, this puts it in a 4-way tie
for 3rd place with the other methods.  IF - the times above still
stand...

And in fact, they don't.  Why?  CPU usage is high on my box right now.
So I baselined the other methods in the 6.0s range, and they are now
coming in at 25s!  So maybe this one is the fastest!  I'll have to do
more
testing.

To be fair, I had to rewrite the former "winner" as:

sub fixsp1a {
${$_[0]}=~s/\A\s+//;
${$_[0]}=~s/\s+\z//;
${$_[0]}=~s/\s+/ /g;
}

using \A and \z.

I wonder how expensive that foreach is.  Knowing that it is exactly
one argument, is there a faster way for this to run, not using
foreach?
Even so, this may not be the fastest trim method - in place, no
pointer,
one line, with the foreach @_ as written.

David


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to