Hi all
I'm looking for a fast way of converting a sequence of four characters (a,b,c,d) to a number.
So far I use
my $lt = [];
$lt->[ord(a)] = 00;
$lt->[ord(b)] = 01;
$lt->[ord(c)] = 10;
$lt->[ord(d)] = 11;
foreach my $string{
_convert($string);
...
sub _convert{
my $string = shift;
my $bs="";
foreach(split(//,$string)){
$bs.=$lt->[$_];
}
return int($bs);
}
I also tried using a hash of form {a=>00,b=>01,c=>10,d=>11} or use four substitutes on $string which should have been kind of four times the runtime (processing the complete string 4 times). But somehow the regexp substitution seems to be the fastest. However, it's still pretty slow if I'm iterating over millions of strings. Any faster solution?
Cheers,
Lowry
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.