public
Last active

  • Download Gist
output.txt
1 2 3 4 5 6 7 8
ok 1 - unpack works
ok 2 - substr works
ok 3 - regex works
1..3
Rate regex unpack substr
regex 298898/s -- -45% -67%
unpack 541032/s 81% -- -40%
substr 899326/s 201% 66% --
string-extract-benchmark.pl
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#!/usr/bin/perl
use strict;
use warnings;
 
use Benchmark qw(cmpthese);
use Test::More;
 
my $data = "100020003000.png";
 
sub using_unpack {
my ($dir, $name, $file, $ext) = unpack "A4A4A4xA3", $data;
return ($dir, $name, $file, $ext);
}
 
sub using_substr {
my $dir = substr $data, 0, 4;
my $name = substr $data, 4, 4;
my $file = substr $data, 8, 4;
my $ext = substr $data, 13, 3;
return ($dir, $name, $file, $ext);
}
 
sub using_regex {
my ($dir, $name, $file, $ext) = $data =~ /^(.{4})(.{4})(.{4})\.(.{3})$/;
return ($dir, $name, $file, $ext);
}
 
is_deeply [ using_unpack() ], [1000, 2000, 3000, "png"], "unpack works";
is_deeply [ using_substr() ], [1000, 2000, 3000, "png"], "substr works";
is_deeply [ using_regex() ], [1000, 2000, 3000, "png"], "regex works";
done_testing;
 
cmpthese( -3, {
unpack => \&using_unpack,
substr => \&using_substr,
regex => \&using_regex,
},
);

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.