"Mark H" <> wrote in news: ups.com:
> I have been beating myself over the head looking for a faster hex to
> ascii routine. I've scoured the Internet for 3 hours now and have
> found nothing that even remotely holds up on megabytes of hex to ascii
> conversion. Here's what I have so far:
> for (my $i = 0; $i < length($file_raw_hex); $i += 2)
> {
> $file_raw.=pack('H2', substr($file_raw_hex, $i, 2));
> }
>
> This is the slowest, coming in at about 2 seconds per meg on a 2.0 Ghz
> P4.
>
> Then this is slightly faster:
> $file_raw_hex =~ s/([a-fA-F0-9]{2})/chr(hex $1)/eg;
>
> Comes in at 1.5 seconds per meg.
>
> But there's got to be something that can do better than this. This is
> a modern CPU, on a modern OS (Linux) with fast SCSI disks.... there is
> no other bottleneck here. This code is dog slow.
How about line-by-line or block-by-block processing?
Here is something quick'n'dirty:
#!/usr/bin/perl
use strict;
use warnings;
open my $in, '<', $ARGV[0] or die "Cannot open '$ARGV[0]': $!";
my ($data, $buffer);
{
local $,;
while (sysread $in, $buffer, 4096) {
my @lines = split /\n/, $buffer;
@lines = map { s{([[

digit:]]{2})}{chr(hex $1)}eg } @lines;
$data .= "@lines";
}
}
__END__
D:\Home\asu1\UseNet\clpmisc\hex> tail -n 3 hexfile
EAFC3885140E9010FFD505127FC20C62F47202C403B9B66F8D C88EC542A0D0888A7522911128B559
BF7E364E624A0651D01BBD4ACFAC813686AF489AC0246DC9CB DFC7D43662AB9D41C3EDEE34AE6DFC
7D402B3CC7D47DF8DF785689AE243A970963E458A6981C20FB 81D13F511DF287CDB11F66C0F2A8FE
D:\Home\asu1\UseNet\clpmisc\hex> dir hexfile
02/08/2006 03:52 PM 2,050,000 hexfile
D:\Home\asu1\UseNet\clpmisc\hex> timethis read.pl hexfile
TimeThis : Command Line : read.pl hexfile
TimeThis : Start Time : Wed Feb 08 16:23:35 2006
TimeThis : End Time : Wed Feb 08 16:23:37 2006
TimeThis : Elapsed Time : 00:00:01.859
which translates to a little less than a second per megabyte on my
AMD64 1.8Ghz laptop (running at 800Mhz on batteries) with Win XPSP2.
See what results you get on your system.
And, please, the next time post a complete program that we can run
by copying and pasting.
--
A. Sinan Unur <>
(reverse each component and remove .invalid for email address)
comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html