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

Re: Sorting characters within a string

by clintp (Curate)
on Aug 24, 2001 at 05:06 UTC ( [id://107580]=note: print w/replies, xml ) Need Help??


in reply to Sorting characters within a string

If we're going for raw speed: don't use perl. :)

If I were doing this in assembly, and I wanted raw speed I'd:

  • Generate all of the possible combinations and their sorted values, like so: AA => AA, AB => AB, BA => AB, AC => AC, CA => AC.
  • Generate code (don't write it by hand!) that does something along the lines of pseudocode which works for aa, ab, ba, and bb:
    if (substr($base,0,1) eq 'a') { if (substr($base,1,1) eq 'a') { return 'aa'; } if (substr($base,1,1) eq 'b') { return 'ab' } } if (substr($base,0,1) eq 'b') { if (substr($base,1,1) eq 'a') { return 'ab' } if (substr($base,1,1) eq 'b') { return 'bb' } }
Which means that for any possible code of length n and an alphabet length q there's only n*q possible comparison/jumps to be made at worst case. (AGCA would be translated to AACG using only 7 comparisons and jumps total for example.)

I'm fairly confident that this would outperform any solution using a hash or a split/join/sort. At least, in assembler. I'm just a little too harried to write code to prove that it might be faster in Perl.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-24 21:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found