{"id":1515,"date":"2009-01-04T16:51:00","date_gmt":"2009-01-04T07:51:00","guid":{"rendered":"https:\/\/hdmr.org\/d\/?e=237"},"modified":"2009-01-04T16:51:00","modified_gmt":"2009-01-04T07:51:00","slug":"blogn-237","status":"publish","type":"post","link":"https:\/\/hdmr.org\/d\/?e=237","title":{"rendered":"\u7f6e\u63db\u304b\u30de\u30c3\u30c1\u304b\u3001\u5bfe\u6c7a\u3057\u3066\u307f\u3088\u3046\u304b"},"content":{"rendered":"<p>Perl\u3067\u30de\u30c3\u30c1\u3057\u305f\u6570\u3092\u6570\u3048\u308b\u306b\u306f\u3069\u3046\u3059\u308c\u3070\u3044\u3044\u304b\u3068\u3044\u3046\u8a71\u304c\u3042\u308b\u3002<br \/>&#8220;123 51 5 65 25 14 25 12 54 65&#8243;\u306e\u4e2d\u306b\u3069\u308c\u3060\u3051\\d+\u3068\u3044\u3046\u6b63\u898f\u8868\u73fe\u306b\u30de\u30c3\u30c1\u3059\u308b\u306e\u304c\u3042\u308b\u304b\u3068\u3044\u3046\u3053\u3068\u3002<br \/>\u4eca\u307e\u3067\u3069\u3046\u3084\u3063\u3066\u8abf\u3079\u308c\u3070\u3044\u3044\u304b\u77e5\u3089\u3093\u304b\u3063\u305f\u306e\u3088\u306d\u3002<br \/>\u3067\u3001\u3069\u3046\u3082\u8907\u6570\u7f6e\u63db\u3057\u305f\u3068\u304d\u306e\u8fd4\u308a\u5024\u304c\u7f6e\u63db\u3057\u305f\u500b\u6570\u3060\u3068\u3044\u3046\u304b\u3089\u3001\u3053\u3046\u3059\u308c\u3070\u3044\u3044\u304b\u306a\u3068\u3002<\/p>\n<pre>print $s=~s\/\\d+\/$&amp;\/g,\"\\n\";<\/pre>\n<p>\u3055\u3063\u304d\u306e\u3092$s\u306b\u5165\u308c\u3066\u304a\u304f\u3068\u300110\u3068\u8868\u793a\u3055\u308c\u308b\u304b\u3089\u300110\u500b\u30de\u30c3\u30c1\u3059\u308b\u3068\u8a00\u3046\u3053\u3068\u3060\u306a\u3002<br \/>g\u3092\u4ed8\u3051\u306a\u3051\u308c\u30701\u3064\u305a\u3064\u3057\u304b\u7f6e\u63db\u3057\u3066\u304f\u308c\u306a\u3044\u306e\u3088\u306d\u3002<br \/>\u305d\u308c\u3067\\d+\u306b\u30de\u30c3\u30c1\u3057\u305f\u3082\u306e\u3092$&#038;\u306b\u7f6e\u63db\u3059\u308b\u3068\u66f8\u3044\u3066\u3042\u308b\u304c\u3001<br \/>$&#038;\u306f\u7279\u6b8a\u5909\u6570\u3067\u3001\u30de\u30c3\u30c1\u3057\u305f\u90e8\u5206\u5168\u90e8\u3068\u3044\u3046\u610f\u5473\u3002<br \/>$1,$2&#8230;\u306f()\u3067\u30b0\u30eb\u30fc\u30d7\u5316\u3055\u308c\u305f\u3082\u306e\u306e1\u756a\u76ee\u30012\u756a\u76ee\u2026\u306e\u610f\u5473\u3060\u3063\u305f\u3051\u3069\u3001<br \/>\u4ed6\u306b\u3082\u30de\u30c3\u30c1\u95a2\u4fc2\u306e\u7279\u6b8a\u5909\u6570\u304c\u3042\u308b\u3051\u3069\u3001\u307e\u3041$&#038;\u306f\u305d\u308c\u306a\u308a\u306b\u4f7f\u3046\u306e\u304b\u306a\u3002<br \/>\u3051\u3069\u3053\u308c\u3063\u3066\u3001123\u306b\u30de\u30c3\u30c1\u3057\u305f\u3089\u3001\u3053\u308c\u3092123\u306b\u7f6e\u63db\u3059\u308b\u3068\u3044\u3046\u610f\u5473\u3060\u304b\u3089\u306d\u3002<br \/>\u7d50\u5c40\u7f6e\u63db\u3057\u3066\u306a\u3044\u306e\u3088\u306d\u3002\u306a\u3093\u3068\u3044\u3046\u52b9\u7387\u306e\u60aa\u3044\u30d7\u30ed\u30b0\u30e9\u30e0\u3002\u3068\u601d\u3046\u308f\u306a\u3002<\/p>\n<hr \/>\n<p>\u305d\u308c\u3067\u3044\u308d\u3044\u308d\u8a66\u3057\u3066\u305f\u306e\u3060\u304c\u3001\u3069\u3046\u3082\u8907\u6570\u30de\u30c3\u30c1\u3068\u3044\u3046\u3084\u3064\u306f2\u3064\u4f7f\u3044\u65b9\u304c\u3042\u308b\u3089\u3057\u3044\u3002<\/p>\n<pre>my @a=( $s=~m\/\\d+\/g );<br \/>print \"$_\\n\" for(@a);<br \/>print \"$&\\n\" while( $s=~m\/\\d+\/g );<\/pre>\n<p>1\u3064\u306f\u50d5\u304c\u4f55\u5ea6\u3082\u3064\u304b\u3063\u3066\u308b\u30de\u30c3\u30c1\u3057\u305f\u7d50\u679c\u3092\u914d\u5217\u306b\u3057\u3066\u304f\u308c\u308b\u3084\u3064\u3002<br \/>()\u3067\u30b0\u30eb\u30fc\u30d7\u5316\u3055\u308c\u3066\u308c\u3070\u3001\u305d\u306e\u5206\u3060\u3051\u3044\u308c\u3066\u304f\u308c\u308b\u3002<br \/>\u7d50\u679c\u3092\u30b9\u30ab\u30e9\u30fc\u3067\u53d7\u3051\u308c\u3070\u30011\u3064\u305a\u3064\u30de\u30c3\u30c1\u3057\u3066\u9032\u3080\u3053\u3068\u304c\u3067\u304d\u308b\u3002<br \/>\u305d\u308c\u304c3\u884c\u76ee\u306ewhile\u3092\u4f7f\u3063\u305f\u3084\u3064\u3002\u4eca\u307e\u3067\u77e5\u3089\u3093\u304b\u3063\u305f\u306e\u3088\u306d\u3002<br \/>\u3053\u308c\u306f\u3001\u307e\u305a123\u306b\u30de\u30c3\u30c1\u3057\u3066\u3001\u6b21\u306b51\u306b\u30de\u30c3\u30c1\u3057\u3066\u2026\u6700\u5f8c\u306b\u30de\u30c3\u30c1\u3057\u306a\u304f\u306a\u308c\u3070\u507d\u3092\u8fd4\u3059\u3068\u3002<br \/>\u307e\u3041Perl\u3068\u3044\u3046\u3084\u3064\u306f\u30b9\u30ab\u30e9\u30fc\u3067\u53d7\u3051\u308b\u304b\u914d\u5217\u3067\u53d7\u3051\u308b\u304b\u3067\u52d5\u4f5c\u304c\u5909\u308f\u3063\u3066\u3057\u307e\u3046\u3053\u3068\u304c\u591a\u3044\u304b\u3089\u306d\u3002<br \/>@a\u3092\u30b9\u30ab\u30e9\u30fc\u3068\u3057\u3066\u53d6\u308a\u6271\u3048\u3070@a\u306e\u8981\u7d20\u6570\u3060\u3057\u3001<>\u3092\u30b9\u30ab\u30e9\u30fc\u3068\u3057\u3066\u53d6\u308a\u6271\u3048\u3070\u4e00\u884c\u8aad\u307f\u3053\u307f\u3060\u3057\u2026<br \/>\u307e\u3041\u305d\u3046\u3044\u3046\u3053\u3068\u304c\u8907\u6570\u30de\u30c3\u30c1\u306b\u3082\u3042\u3063\u305f\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u3002<br \/>\u3057\u304b\u3057\u3053\u308c\u3092\u4f7f\u3048\u3070\u500b\u6570\u3092\u6570\u3048\u308b\u3053\u3068\u3092\u3053\u3046\u304b\u3051\u308b\u3002<\/p>\n<pre>my $n=0;<br \/>$n++ while( $s=~m\/\\d+\/g );<br \/>print \"$n\\n\";<\/pre>\n<p>\u3053\u3063\u3061\u306e\u65b9\u304c\u6027\u80fd\u304c\u3044\u3044\u306b\u9055\u3044\u306a\u3044\u3002<br \/>\u306a\u3089\u78ba\u304b\u3081\u3066\u307f\u3088\u3046\u3002<\/p>\n<hr \/>\n<p>\u3068\u3044\u3046\u308f\u3051\u3067Benchmark\u30e2\u30b8\u30e5\u30fc\u30eb\u3067\u8a66\u3057\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<pre>use strict;<br \/>use warnings;<br \/>use Benchmark;<br \/>my $s='123 51 5 65 25 14 25 12 54 65 'x1000;<br \/>timethese(500,{<br \/>  'replace'=>sub{ $s=~s\/\\d+\/$&\/g; },<br \/>  'match'  =>sub{ my $n=0; $n++ while( $s=~m\/\\d+\/g ); }<br \/>  } );<\/pre>\n<p>\u3053\u3093\u306a\u4fbf\u5229\u306a\u3082\u306e\u304c\u3042\u308b\u3093\u3067\u3059\u306d\u3002<br \/>timethese\u95a2\u6570\u306b\u8a66\u9a13\u3059\u308b\u56de\u6570\u3068\u8a66\u9a13\u540d\u3068\u30b5\u30d6\u30eb\u30fc\u30c1\u30f3\u30ea\u30d5\u30a1\u30ec\u30f3\u30b9\u306e\u30cf\u30c3\u30b7\u30e5\u30ea\u30d5\u30a1\u30ec\u30f3\u30b9\u3092\u6e21\u3059\u3068\u3002<br \/>\u4eca\u56de\u30bf\u30fc\u30b2\u30c3\u30c8\u3068\u3057\u305f\u6587\u5b57\u5217\u306f&#8217;123 51 5 65 25 14 25 12 54 65 &#8216;\u30921000\u56de\u7e70\u308a\u8fd4\u3057\u305f\u3082\u306e\u3002<br \/>\u3053\u306e\u8a66\u9a13\u3092500\u56de\u3084\u3063\u3066\u3082\u3089\u3063\u305f\u3068\u3002<br \/>\u672c\u5f53\u306f1000\u56de\u3050\u3089\u3044\u3084\u308c\u3070\u3088\u304b\u3063\u305f\u3093\u3060\u304c\u3001\u5b9f\u306f\u7d50\u69cb\u6642\u9593\u304c\u304b\u304b\u308b\u3002<br \/>\u5f90\u3005\u306b\u5897\u3084\u3057\u306a\u304c\u3089\u3084\u3063\u305f\u306e\u3060\u304c\u3001\u307e\u3041500\u56de\u3050\u3089\u3044\u3084\u308c\u3070\u3044\u3044\u304b\u3063\u3066\u3002<br \/>\u7d50\u679c\u3067\u3059\u304c\u2026<\/p>\n<pre>Benchmark: timing 500 iterations of match, replace...<br \/>     match: 35 wallclock secs (33.88 usr +  0.00 sys = 33.88 CPU) @ 14.76\/s (n=500)<br \/>   replace:  6 wallclock secs ( 6.11 usr +  0.00 sys =  6.11 CPU) @ 81.85\/s (n=500)<\/pre>\n<p>\u5b9f\u306freplace\u306e\u65b9\u304c\u5727\u5012\u7684\u306b\u901f\u3044\u3093\u3067\u3059\u306d\u3002<br \/>\u4f55\u3067\u306a\u3093\u3060\u308d\u3046\u306d\u3002\u3051\u3069\u4e0d\u601d\u8b70\u306a\u8a71\u3067\u3059\u3088\u306d\u3002<br \/>\u7f6e\u63db\u306e\u65b9\u306f\u7e70\u308a\u8fd4\u3057\u306b\u7279\u5316\u3055\u308c\u3066\u308b\u304b\u3089\u304b\u306a\u3002<\/p>\n<hr \/>\n<p>\u305d\u308c\u306a\u3089\u3070\u3068\u3053\u306e2\u3064\u306e\u30b3\u30fc\u30c9\u3092\u6bd4\u8f03\u3057\u3066\u307f\u308b\u3053\u3068\u306b\u3057\u305f\u3002<\/p>\n<pre>my @a1=();<br \/>while($s =~ m\/^(.+?)\\s+(.+)$\/mg){<br \/>  push @a1,[$1,$2];<br \/>}<br \/>my @a2=();<br \/>$s =~ s\/^(.+?)\\s+(.+)$\/<br \/>  push @a2,[$1,$2]; $&;<br \/>\/emg;<\/pre>\n<p>m\u3092\u4ed8\u3051\u308b\u3068\u8907\u6570\u884c\u306e\u6587\u5b57\u5217\u306e\u6539\u884c\u306b\u3082^\u3068$\u304c\u4f7f\u3048\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u304b\u3089\u306d\u3002<br \/>\u884c\u3054\u3068\u306b\u3001test 100\u3068\u3044\u3046\u884c\u306a\u3089[&#8216;test&#8217;,&#8217;100&#8242;]\u3068\u3044\u3046\u914d\u5217\u30ea\u30d5\u30a1\u30ec\u30f3\u30b9\u3092\u4f5c\u3063\u3066\u914d\u5217\u306b\u6295\u3052\u8fbc\u3080\u3068\u3002<br \/>1\u3064\u76ee\u306f\u8907\u6570\u30de\u30c3\u30c1\u306e\u65b9\u6cd5\u3002\u307e\u3041\u305d\u306e\u307e\u307e\u3067\u3059\u306d\u3002<br \/>2\u3064\u76ee\u306f\u7f6e\u63db\u306be\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u3064\u3051\u305f\u3082\u306e\u3002<br \/>e\u30aa\u30d7\u30b7\u30e7\u30f3\u3068\u3044\u3046\u306e\u306f\u3001\u7f6e\u63db\u5148\u3092Perl\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u3068\u3057\u3066\u8a55\u4fa1\u3059\u308b\u3082\u306e\u3002<\/p>\n<pre>$s =~ s\/0x([0-9a-fA-F]+)\/hex($1)\/eg;<\/pre>\n<p>\u3053\u308c\u306f0x5A\u3068\u304b\u306b\u30de\u30c3\u30c1\u3057\u3066\u3001\u3053\u308c\u3092hex(&#8216;5A&#8217;)\u306e\u7d50\u679c\u3067\u3082\u3063\u3066\u300190\u306b\u7f6e\u63db\u3059\u308b\u3068\u3002<br \/>\u305d\u308c\u3067\u3053\u308c\u306f\u30de\u30c3\u30c1\u3057\u305f\u3082\u306e\u3092push @a2,[$1,$2]; $&#038;;\u306e\u7d50\u679c\u306b\u7f6e\u63db\u3059\u308b\u3068\u3044\u3046\u3082\u306e\u3060\u304c\u3001<br \/>\u7d50\u5c40\u3053\u308c\u306e\u7d50\u679c\u3063\u3066$&#038;\u306a\u3093\u3060\u3088\u306d\u3002\u3060\u304b\u3089\u4f55\u3082\u7f6e\u63db\u3057\u306a\u3044\u7f6e\u63db\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u3002<br \/>\u3055\u3066\u3001\u6bd4\u8f03\u3057\u3066\u307f\u3088\u3046\u304b\u3002<\/p>\n<pre>my $s=\"test 100\\n\"x10000;<br \/>timethese(100,{<br \/>  'match'  =>sub{ my @a1=(); while($s =~ m\/^(.+?)\\s+(.+)$\/mg){ push @a1,[$1,$2]; } @a1; },<br \/>  'replace'=>sub{ my @a2=(); $s =~ s\/^(.+?)\\s+(.+)$\/ push @a2,[$1,$2]; $&; \/emg; @a2; }<br \/>  } );<\/pre>\n<p>test 100\u306810000\u884c\u7e70\u308a\u8fd4\u3057\u305f\u3082\u306e\u306b\u51e6\u7406\u3059\u308b\u3053\u3068\u3092100\u56de\u3059\u308b\u30d9\u30f3\u30c1\u30de\u30fc\u30af\u306d\u3002<br \/>\u7d50\u679c\u3060\u304c\u3001\u3053\u3093\u306a\u98a8\u306b\u306a\u3063\u305f\u3002<\/p>\n<pre>Benchmark: timing 100 iterations of match, replace...<br \/>     match: 39 wallclock secs (37.77 usr +  0.03 sys = 37.80 CPU) @  2.65\/s (n=100)<br \/>   replace:  9 wallclock secs ( 8.19 usr +  0.11 sys =  8.30 CPU) @ 12.05\/s (n=100)<\/pre>\n<p>\u3068\u3001e\u30aa\u30d7\u30b7\u30e7\u30f3\u4ed8\u304d\u306e\u7f6e\u63db\u306e\u65b9\u304c\u9ad8\u901f\u3067\u3057\u305f\u3002<br \/>\u3057\u304b\u30571000\u884c\u306a\u3089\u5c11\u3057\u901f\u3044\u304b\u306a\u3068\u3044\u3046\u7a0b\u5ea6\u3001100\u884c\u3060\u3068\u30de\u30c3\u30c1\u306e\u65b9\u304c\u901f\u3044\u3050\u3089\u3044\u3002<br \/>\u666e\u6bb51000\u884c\u3082\u51e6\u7406\u3057\u306a\u3044\u3060\u308d\u3046\u3057\u3001\u597d\u304d\u306a\u65b9\u3067\u66f8\u3051\u3070\u3088\u3055\u305d\u3046\u3067\u3059\u306d\u3002<br \/>\u305d\u3057\u3066\u3068\u3093\u3067\u3082\u306a\u3044\u56de\u6570\u306e\u7e70\u308a\u8fd4\u3057\u306b\u306f\u8907\u6570\u7f6e\u63db\u304c\u5411\u3044\u3066\u3044\u308b\u3053\u3068\u304c\u3088\u304f\u308f\u304b\u308a\u307e\u3057\u305f\u306d\u3002<\/p>\n<hr \/>\n<p>\u307e\u3041Perl\u3068\u3044\u3046\u306e\u306f\u3084\u308a\u65b9\u306f\u3044\u308d\u3044\u308d\u3042\u308b\u308f\u3051\u3067\u3001\u597d\u304d\u306a\u65b9\u6cd5\u3067\u66f8\u3051\u3070\u3044\u3044\u306e\u306f\u78ba\u304b\u3067\u3059\u306d\u3002<br \/>\u3051\u3069\u6027\u80fd\u304c\u3044\u3044\u3068\u304b\u6b20\u304d\u3084\u3059\u3044\u3068\u304b\u3042\u308b\u304b\u3089\u3001\u3053\u308c\u304c\u3044\u3044\u3067\u3059\u306d\u3068\u3044\u3046\u306e\u3082\u3042\u308b\u3068\u3002<br \/>\u50d5\u306e\u611f\u60f3\u3067\u306f\u500b\u6570\u6570\u3048\u308b\u306e\u306f\u7f6e\u63db\u306e\u65b9\u304c\u3044\u3044\u3067\u3059\u306d\u3001<br \/>\u3051\u3069\u5225\u306b\u305d\u308c\u4ee5\u5916\u306f\u308f\u304b\u308a\u306b\u304f\u3044\u66f8\u304d\u65b9\u3057\u3066\u3084\u308b\u5fc5\u8981\u3082\u306a\u3044\u3060\u308d\u3046\u3068\u3002<br \/>\u660e\u3089\u304b\u306b\u898b\u306b\u304f\u3044\u3057\u306d\u3047\u3002\u4f55\u3057\u3066\u308b\u3093\u3060?\u3068\u601d\u308f\u308c\u308b\u3053\u3068\u9593\u9055\u3048\u306a\u3057\u3002<br \/>\u305f\u3060\u77e5\u3063\u3066\u304a\u304f\u3068\u9762\u767d\u3044\u3068\u306f\u601d\u3046\u3051\u3069\u306d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Perl\u3067\u30de\u30c3\u30c1\u3057\u305f\u6570\u3092\u6570\u3048\u308b\u306b\u306f\u3069\u3046\u3059\u308c\u3070\u3044\u3044\u304b\u3068\u3044\u3046\u8a71\u304c\u3042\u308b\u3002&#8220;123 51 5 65 25 14 25 12 54 65&#8243;\u306e\u4e2d\u306b\u3069\u308c\u3060\u3051\\d+\u3068\u3044\u3046\u6b63\u898f\u8868\u73fe\u306b\u30de\u30c3\u30c1\u3059\u308b\u306e\u304c\u3042\u308b\u304b\u3068\u3044\u3046\u3053\u3068\u3002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-1515","post","type-post","status-publish","format-standard","hentry","category-computer"],"_links":{"self":[{"href":"https:\/\/hdmr.org\/d\/index.php?rest_route=\/wp\/v2\/posts\/1515","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hdmr.org\/d\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hdmr.org\/d\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hdmr.org\/d\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hdmr.org\/d\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1515"}],"version-history":[{"count":0,"href":"https:\/\/hdmr.org\/d\/index.php?rest_route=\/wp\/v2\/posts\/1515\/revisions"}],"wp:attachment":[{"href":"https:\/\/hdmr.org\/d\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1515"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hdmr.org\/d\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1515"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hdmr.org\/d\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}