encode/decode ãã find_encoding ã®ãéãã®ã¯ç¥ã£ã¦ããã©ããã¾ã§ã
ä»æ´ãã¿ãç¶ãã
ãããããdecode()ã¨encode()ããå ´åã«ã¯ãOOã¤ã³ã¿ã¼ãã§ã¼ã¹ã使ã£ãæ¹ãé«éã§ãããªããªã(de|en)codeãæåã³ã¼ãåã解決ããæéããªããªãããã§ãã
404 Blog Not Found:perl - Encode 入門
ã¨è¨ããã¦ããã®ã¯ç¥ã£ã¦ããã©å®éã¯æ¯å decode('euc-jp', $str) ã¨ããã¦ã¦å®éã©ããããéãã®ããªã¼ã¨ãµã¨æã£ã¦
use strict; use warnings; use Benchmark qw/:all/; use Encode qw/find_encoding encode decode/; my $euc = find_encoding('euc-jp'); my $utf = find_encoding('utf-8'); open my $fh, "<", "euc.txt"; my @txt; for my $in (<$fh>) { push @txt, $in; } close $fh; open my $normal, ">>", "normal.out"; open my $name, ">>", "name.out"; cmpthese( timethese(0, { normal => sub {normal()}, name => sub {name()}, }) ); sub normal { my $out; for my $in (@txt) { $out = decode('euc-jp', $in); $out = encode('utf-8', $out); print $normal $out; } } sub name { my $out; for my $in (@txt) { $out = $euc->decode($in); $out = $utf->encode($out); print $name $out; } }
% perl encode.pl Benchmark: running name, normal for at least 3 CPU seconds... name: 3 wallclock secs ( 2.97 usr + 0.03 sys = 3.00 CPU) @ 11600.33/s (n=34801) normal: 4 wallclock secs ( 3.22 usr + 0.00 sys = 3.22 CPU) @ 1641.61/s (n=5286) Rate normal name normal 1642/s -- -86% name 11600/s 607% --
ã¨ãåã¾ããçµæã«ãªã£ãã®ã§ããããã¯ourå¤æ°ã«find_encodingæã£ã¦ã¢ããªå
¨ä½ã§ä½¿ãã¾ãããããªã¨æã£ããã©
2ç§èãã¦ãããã
èªåä¸äººã§ã¼ãããä½ãå ´åãªãããã