Skip to content

Instantly share code, notes, and snippets.

@dankogai
Last active December 14, 2015 11:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dankogai/5080017 to your computer and use it in GitHub Desktop.
Save dankogai/5080017 to your computer and use it in GitHub Desktop.
Encode vs. Unicode::UTF8 vs. '<:utf8'
#!/usr/bin/env perl
use strict;
use warnings;
use Encode qw();
use Unicode::UTF8 qw();
use Benchmark qw/:all/;
my $enc = Encode::find_encoding('utf8');
# Decode file
cmpthese timethese - 1, {
'$e->decode' => sub {
my $line;
open my $fh, '<', $0 or die "$0:$!";
$line = $enc->decode($_) while <$fh>;
close $fh;
},
'E::d_utf8' => sub {
my $line;
open my $fh, '<', $0 or die "$0:$!";
$line = Encode::decode_utf8($_) while <$fh>;
close $fh;
},
'u8::decode' => sub {
my $line;
open my $fh, '<', $0 or die "$0:$!";
utf8::decode($_) and $line = $_ while <$fh>;
close $fh;
},
'<:utf8' => sub {
my $line;
open my $fh, '<:utf8', $0 or die "$0:$!";
$line = $_ while <$fh>;
close $fh;
},
'<:e(utf8)' => sub {
my $line;
open my $fh, '<:encoding(utf8)', $0 or die "$0:$!";
$line = $_ while <$fh>;
close $fh;
},
'U::U8::d_utf8' => sub {
my $line;
open my $fh, '<', $0 or die "$0:$!";
$line = Unicode::UTF8::decode_utf8($_) while <$fh>;
close $fh;
},
};
__DATA__
もう、三十七歳になります。こないだ、或る先輩が、
よく、まあ、君は、生きて来たなあ、
としみじみ言っていました。
私自身にも、三十七まで生きて来たのが、うそのように思われる事があります。
Rate E::d_utf8 $e->decode <:e(utf8) u8::decode U::U8::d_utf8 <:utf8
E::d_utf8 3901/s -- -35% -66% -86% -86% -88%
$e->decode 5966/s 53% -- -49% -78% -79% -81%
<:e(utf8) 11605/s 197% 95% -- -57% -60% -64%
u8::decode 27048/s 593% 353% 133% -- -6% -15%
U::U8::d_utf8 28710/s 636% 381% 147% 6% -- -10%
<:utf8 31811/s 715% 433% 174% 18% 11% --
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment