Breaking this kind of encryption is downright trivial. People who know what they are doing can do it by hand in a matter of minutes based on character frequency. If you want encryption, I strongly recommend looking for an appropriate module.

But it looks like you knew that. Even without formatting (the <code> tag exists for a reason) it looks like you are trying to do ROT13. The problem you have is that you have a lot of this or this or this or... logic which has to be scanned on each character. It is far faster to arrange to have a hash that contains the answer and do a lookup. So the key logic would be:

my $encrypted = join '', map {exists $trans{$_} ? $trans{$_} : $_} spl +it //, $orig;
Of course the tr function gives an even more efficient solution in this case.

But the general performance note is that any time you can convert scanning logic (scanning a string, array, file, whatever) to a hash lookup, you moved to a more efficient algorithm and if the thing scanned was long should see significant performance improvments.


In reply to Re (tilly) 1: More Efficient Subroutine by tilly
in thread More Efficient Subroutine by jcr

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.