I ran the following quick Benchmark code to test:
#!/usr/bin/perl -w
use strict;
use Benchmark;
my $input = 'X' x 65; #test input
my @result;
timethese(-10, {
'Regex' => sub {
@result = ($input =~ /(.{16})(.{20})(.{20})(.{1})(.
+{8})/);
# print join "\t", @result;
},
'Unpack' => sub {
@result = unpack "A16A20A20AA8",$input;
# print join "\t", @result;
},
});
which yielded:
Benchmark: running Regex, Unpack, each for at least 10 CPU seconds...
Regex: 9 wallclock secs (10.44 usr + 0.00 sys = 0.44 CPU) @ 198
+59.10/s (n=207329)
Unpack: 11 wallclock secs (10.17 usr + 0.00 sys = 0.17 CPU) @ 333
+12.29/s (n=338786)
It would seem unpack is more efficient in this case.
-
ase