[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:37:54PM -0500, Jeff Pinyan wrote:
> On Dec 1, Ronald J Kimball said:
> 
> >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");
> >}
> 
> That's almost identical to the algorithm I almost finished, when I got
> your damned email. ;)

Good, that means I'm still a few milliseconds ahead. ;)

sub y3 {		# rjk slightly modified, really
  my $string = shift;

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

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

  substr($string, ($l + ($x<$y?$x:(-$y))) , 1, "\n");
}

sub y4			# look more juicy, but take more time. Oh well. :/
{
        my ($string, $l, $x, $y ) = ( shift );

        $l = length($string) / 2;

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

        substr $string, ($x + $y < 2 * $l )?$y:$x, 1, "\n";
}


Benchmark: running rjk, y3, y4, each for at least 20 CPU seconds...
       rjk: @ 6019.29/s (n=120446)
        y3: @ 6460.52/s (n=129275)
        y4: @ 6098.32/s (n=123186)


Joy,
Yanick

-- 
"Just friends" means "I don't just want you to mourn the loss; I want 
to remind you of it every day. I want you to suffer. I want you to 
envy. I want you to die slowly, a bit at a time. And I want you to 
smile and thank me for it." - someone on alt.gothic

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