Created
October 8, 2009 06:10
-
-
Save arodland/204800 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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% -- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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