Rather than split $ucrypt into bytes, you could unpack it into an array of integers. This would avoid allocating length($ucrypt) strings. If you use an unsigned character template (C), you can continue to compare it to 127.

You might get even better performance if you use a multibyte template character appropriate to your architecture (perhaps, N or Q). With an appropriate bitmask, you could use a bitwise AND (&) on each value and compare it to 0. This would test 4 or 8 bytes at once.

Update:

The basic idea was to replace:

@uchar = split(//,$ucrypt);
with:
my @vals = unpack('C*', $ucrypt);
The list @vals gets the values of each "ord($uchar$i)". You then test for an element that is greater than 127.

You potentially get better performance by bundling the tests. Since I have a 32 bit architecture, I'd create a 32 bit bitmask to select the high bits of each byte, unpack 4 bytes at a time, and logically AND the result with the mask:

my $mask = 0x80808080; ... my @vals = map { $_ & $mask } unpack('N*', $ucrypt);
Now, the test fails if any element of @vals is not zero. The potential improvement comes from doing one logical AND and one integer comparison per 4 bytes rather than one integer comparison per 1 byte.

In reply to Re: RC4 cipher performance by eye
in thread RC4 cipher performance by jaiello

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.