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

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



Hullo,

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? :)
 
Joy,
Yanick

-- 
$_ = q<Yap aqsueap Ritj d^dohr*>   and s(\S)
{chr int (5 *sin$i ++)+ ord $& }ge and print

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