Skip to content

@arodland /output.txt
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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% --
#!/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,
},
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.