[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:
> 
> 	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? :)

That's too much work.  (And watch out for infinite loops on degenerate
input.)


sub rjk {
  my $string = shift;

  my $l = length($string)/2;

  my $x = abs($l - index($string, ' ', $l));
  my $y = abs($l - rindex($string, ' ', $l));

  my $p;

  if ($x < $y) {
    $p = $l + $x;
  } else {
    $p = $l - $y;
  }

  substr($string, $p, 1, "\n");
}

Benchmark: running japhy, rjk, y2, each for at least 10 CPU seconds...
     japhy: @ 18991.82/s (n=195046)
       rjk: @ 53091.80/s (n=530918)
        y2: @ 44838.41/s (n=468113)

Ronald

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