in reply to Re: Re: Peeling the Peelings
in thread Peeling the Peelings

As my original attempt was so crap, I thought I'd take another crack. Abandoning any attempt at validation and avoiding the regex engine completely, I came up with this which seems to be about 15% better than the best so far.

sub peel { my( $s, $n ) = @_; my( $start, $stop ) = ( 0, length $s ); ($start,$stop) = ( 1 + index( $s, '(', $start ), rindex( $s, ')', $stop -1 ) # reformatting_for_posting error corrected # Was ) while $n-- > 0 and index( $s, '(', $start +1 ) > 0; ) while $n-- and index( $s, '(', $start +1 ) > 0; substr $s, $start, $stop - $start; } Rate mine-less-lgnt buk2 mine-less-lgnt 918/s -- -15% buk2 1075/s 17% --

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


Replies are listed 'Best First'.
Re: Re: Re: Re: Peeling the Peelings
by bobn (Chaplain) on Jul 03, 2003 at 13:18 UTC
    Not bad, but pluggiing it into Aristotle's magic machine (at here) yields:
    not ok 8 - by substr for case -1 # Failed test (./t53.pl at line 90) # got: 'hello(what(is(this(all(about)))))' # expected: 'about'
    It's easy to be fast when you don't cover all the cases. ;-P

    --Bob Niederman, http://bob-n.com

      Somehow I introduced an error into the code. This is the code that produced the benckmark shown. The difference is the first condition on the while line. I'd did have to reformat the code when pasting to stop it wrapping as I edit my code using lines well over 100 chars. Quite how I managed to add the > 0 in there I can't explain. This does handle all cases. I'll update the original node to reflect the mistake.

      sub buk2 { my( $s, $n ) = @_; my( $start, $stop ) = ( 0, length $s ); ($start,$stop) = ( 1 + index( $s, '(', $start ), rindex( $s, ')', $stop -1 ) ) while $n-- and index( $s, '(', $start +1 ) > 0; substr $s, $start, $stop - $start; }
        And it looks like we have a new winner:
        Benchmark: timing 50000 iterations of Aristotle, Fatvamp, bobn, buk2.. +. Aristotle: 8 wallclock secs ( 9.22 usr + 0.00 sys = 9.22 CPU) @ 54 +22.99/s (n=50000) Fatvamp: 12 wallclock secs (12.30 usr + 0.00 sys = 12.30 CPU) @ 40 +65.04/s (n=50000) bobn: 8 wallclock secs ( 9.68 usr + 0.00 sys = 9.68 CPU) @ 51 +65.29/s (n=50000) buk2: 8 wallclock secs ( 8.42 usr + 0.00 sys = 8.42 CPU) @ 59 +38.24/s (n=50000) Benchmark: timing 50000 iterations of Aristotle, Fatvamp, bobn, buk2.. +. Aristotle: 11 wallclock secs ( 8.96 usr + 0.01 sys = 8.97 CPU) @ 55 +74.14/s (n=50000) Fatvamp: 17 wallclock secs (13.16 usr + 0.06 sys = 13.22 CPU) @ 37 +82.15/s (n=50000) bobn: 10 wallclock secs (10.29 usr + 0.01 sys = 10.30 CPU) @ 48 +54.37/s (n=50000) buk2: 8 wallclock secs ( 8.35 usr + 0.01 sys = 8.36 CPU) @ 59 +80.86/s (n=50000)
        Not sure why the variance, but in any case, you've edged out Aristotle.

        --Bob Niederman, http://bob-n.com