[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [jeffp@crusoe.net: [FWP] string evenness]



On Fri, Dec 01, 2000 at 05:48:41PM -0500, yanick1@sympatico.ca wrote:
> On Fri, Dec 01, 2000 at 04:11:11PM -0500, Jeff Pinyan wrote:
> > On Dec 1, yanick1@sympatico.ca said:
> > 
> > >>  $half = int(length($str)/2);
> > >>  $str =~ s{( ?)([^ ]*)(?<=^.{$half})([^ ]*)( ?)}
> > >>           { length($2) < length($3) ? "\n$2$3$4" : "$1$2$3\n" }e;
> > >
> > >Isn't 
> > >
> > >	$_ = 'extraterrestrial space monkeys';
> > >	$l = length $_;
> > >	$l-- or last until s/(.{$l,})( )(.{$l,})/$1\n$3/;
> > >
> > >simplier? :)
> > 
> > Yes, but slower:
> > 
> >   Benchmark: running japhy, yanick, each for at least 5 CPU seconds...
> >      japhy:  @ 2902.59/s (n=14542)
> >     yanick:  @  150.28/s (n=  819)
> > 
> > I don't find that reduction in speed as "fun". ;)  20 times slower is
> > cause for concern in my book.  And the reason for the serious drop in
> > speed in yours is because the regex must be recompiled (over and over).
> 
> 	Oh, if you prefer it that way:
> 
> sub y2
> {
> 	my $string = shift;
>  
> 	my( $l, $x, $y );
> 	$l = length($string)/2;
>  
> 	while(1)
> 	{
> 		$x = index( $string, ' ', $x+1 );
> 		if( $l - $x > 0 )
> 		{
> 			$y = $x;
> 		}
> 		else
> 		{
> 			$y = $x if $x + $y < 2 * $l;
> 			last;
> 		}
> 	}
> 
> 	substr $string, $y, 1, "\n";
> }
> 
> Benchmark: running japhy, y1, y2, each for at least 10 CPU seconds...
>      japhy: @ 8950.87/s (n=93089)
>         y1: @ 546.80/s (n=5468)
>         y2: @ 13483.00/s (n=134830)
> 
> 	Does this qualify as funnier? :)

Same thing, but shorter and quicker:

sub y2
{
	my ($string, $l, $x, $y ) = ( shift );

	$l = length($string) / 2;

	 $y = $x while ($x = index( $string, ' ', $x+1 )) < $l;
	 $y = $x if $x + $y < 2 * $l;

	substr $string, $y, 1, "\n";
	$string;
}

Benchmark: running japhy, y1, y2, each for at least 10 CPU seconds...
     japhy: @ 9056.82/s (n=91021)
        y1: @ 556.73/s (n=5751)
        y2: @ 15716.83/s (n=160626)


Joy,
Yanick

-- 
@_=map hex hex||' ',qw-f4240 1388a 0 14ff1 50 4e2a 0 1f40 
4e2a 320 0 50 195 4e2a-;for(@_){($i,$e)=('a',($_+0)?'':$_
);do{$_&1and$e.=$i}while($_>>=$i++**0);print reverse$e;}

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe