Beefy Boxes and Bandwidth Generously Provided by pair Networks Russ
Welcome to the Monastery
 
PerlMonks  

Re: Finding longest palindrome from a string

by Aristotle (Chancellor)
on Aug 14, 2004 at 01:50 UTC ( #382881=note: print w/ replies, xml ) Need Help??


in reply to Finding longest palindrome from a string

A slight modification of my previous entry yields the winning entry (so far). :-)

sub { my $str = shift; my $rts = reverse $str; my $palindrome = ''; my $minlen = 3; for my $rotate_count ( 0 .. length( $str ) - 1 ) { my $mask = $str ^ $rts; # to distinguish adjacent palindromes substr $mask, $rotate_count, 0, "\1"; while ( $mask =~ /\0{$minlen,}/g ) { my $offs = $-[0]; --$offs if $offs > $rotate_count; # compensate for mark +er $palindrome = substr $str, $offs, $+[0] - $-[0]; $minlen = 1 + length $palindrome; } substr $rts, 0, 0, chop $rts; } return $palindrome; }

Makeshifts last the longest.


Comment on Re: Finding longest palindrome from a string
Download Code
Re^2: Finding longest palindrome from a string
by Aristotle (Chancellor) on Aug 14, 2004 at 02:09 UTC

    And here's the proof (sorry it's so wide).

    Note that I had to disqualify some entries for failing the tests.

    The full benchmark I ran follows. Note that some entries required slight modifications in order to compile with strictures and run without warnings. I paid careful attention to keep the semantics intact, but if I disqualified your entry, please check my copy of your code for potential breakage.

    Makeshifts last the longest.

Re^2: Finding longest palindrome from a string
by ccn (Vicar) on Aug 14, 2004 at 08:44 UTC

      Oops, thank you. I missed that one because I downloaded the solutions by crawling the displaycode links on the thread page and he didn't put his entry in CODE tags.

      It works correctly and therefor qualifies, but I only get about 340 iterations/s for it. That puts it next to Limbic~Region's entry in my chart. I'm not surprised, as his code is quite complex and involves a true for(;;) loop (relatively slow in Perl). Fast code in Perl means as few opcodes as possible and letting builtins do work implicitly as much as possible. (See GRT, f.ex.)

      That is how I arrived at my second version. I copied the first version, ripped everything except the XOR out of the loop, and started benchmarking them against each other as I tried to accelerate extraction of null runs from the bitmask and rotation of the reverse string. Every single operation I added to the (non-functional, skeletal) second version had a dramatic impact on speed. Whatever I did, I found nothing with which to improve upon while( /\0{3,}/g ) { } and substr+chop. I only managed to get a speedup when I constrained the match further so that the regex engine isn't exited to drop into the loop body for null runs that are too short to be candidates. Apparently, avoiding that penalty by skipping matches implicitly more than makes up for the additional cost of having to compile the regex multiple times, which skipping them explicitly didn't require.

      The version and compile-time flags of the Perl in use probably matter to some degree, as well.

      Makeshifts last the longest.

        Your benchmarking is not fair. The place is hihgly dependend on input data. E.g. if you use only     'abcdedcbabcdefgfedcbabcdefghijklmnonmlkjihgfedcbabcdefghijklkjihgfedcbabcdefghijklmnoponmlkjihgfedcba' you get other results.

        I can't imagine a fair benchmark because it must go through all possible permutations for short and very very long strings. Or it must use random input data and calculate the statistics.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://382881]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (9)
As of 2014-04-18 13:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (468 votes), past polls