in reply to RC4 cipher performance

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.

Replies are listed 'Best First'.
Re^2: RC4 cipher performance
by jaiello (Novice) on Nov 17, 2008 at 04:37 UTC
    Can you give me an example in my code?

    Thanks