JavaScript効率PK――特定文字列に出現する回数を統計します.
5030 ワード
2011年7月15日23:34効率PK――統計文字列に登場する文字の出現回数
JavaScript replace(RegExp,Function)詳細
var str = "The officials say tougher legislation is needed because some \
telecommunications companies in recent years have begun new services and made \
system upgrades that create technical obstacles to surveillance. They want to \
increase legal incentives and penalties aimed at pushing carriers like Verizon, \
AT&T, and Comcast to ensure that any network changes will not disrupt their \
ability to conduct wiretaps." +
"An Obama administration task force that includes officials from the \
Justice and Commerce Departments, the F.B.I. and other agencies recently began \
working on draft legislation to strengthen and expand a 1994 law requiring \
carriers to make sure their systems can be wiretapped. There is not yet \
agreement over the details, according to officials familiar with the \
deliberations, but they said the administration intends to submit a package to \
Congress next year." +
"To bolster their case, security agencies are citing two previously \
undisclosed episodes in which major carriers were stymied for weeks or even \
months when they tried to comply with court-approved wiretap orders in criminal \
or terrorism investigations, the officials said.",
count = 0,
index = 0,
arrStr = [],
oLetter = {};
str = str.replace(/\s/g,''); // Method_3 normal ,
for (var i = 0; i < 5000; i++) { //create a long text
arrStr.push(str);
}
str = arrStr.join(""); // ","? ",", "," 。
if(! ('console' in this || 'console' in window) ){ // console
console = {
stacks : [],
log : function(str){
stacks.push(str);
},
show : function(){
alert(console.stacks.join('
'));
console.stacks = [];
}
}
}
私の方法は、str.replaceを使ってstr.replaceの使い方を遍歴していますので、前のエッセイ「javascriptはどのキャラクターの出現回数が一番多いかを統計します。修正版」を参考にしてください.function method_replace_RegExp_function(){
function counter(match) { //
if(visited[match]){
visited[match]++;
} else {
visited[match] = 1;
}
}
var count = 0, index = 0, arrStr=[], visited = {};
var begin = (new Date()).getTime();
str.replace(/\S/g, counter);
for (var i in visited) {
if (visited[i] > count) {
count = visited[i];
index = i;
}
}
var end = + new Date();
console.log("Method_replace_RegExp_Function:
" + index + ", " + count + " ", " :" + (end - begin) + " ");
}
// Normal
function method_normal(){
var count = 0, index = 0, arrStr = [], visited = {}, tmp = '';
var begin = (new Date()).getTime();
for(var i = 0; i < str.length; i++){
tmp = str.charAt(i);
if(visited[tmp]){
visited[tmp]++;
} else {
visited[tmp] = 1;
}
}
for (var i in visited) {
if (visited[i] > count) {
count = visited[i];
index = i;
}
}
var end = + new Date();
console.log("Method_normal:
" + index + ", " + count + " ", " :" + (end - begin) + " ");
}
method_2();
method_3();
method_replace_RegExp_function();
method_normal();
(!!console.show)?console.show():void 0;
// console
いくつかの環境での出力結果: 3.1.3.600
Method_2:
e, 610000 :7128
Method_3:
e, 610000 :6757
Method_replace_RegExp_Function:
e, 610000 :4399
Method_normal:
e, 610000 :5925
Node.exe 2011.07.14 v0.5.1 http://nodejs.org
> method_2();
Method_2:
e, 610000 :3141
> method_3();
Method_3:
e, 610000 :1560
> //method_replace_RegExp_function();
// ……
> method_normal();
Method_normal:
e, 610000 :1045
FireFox 3.6.3 FireBug 1.7.3
Method_2:
e, 610000 :12046
Method_3:
e, 610000 :10488
Method_replace_RegExp_Function:
e, 610000 :6836
Method_normal:
e, 610000 :5351
IE9:
: Method_2:
e, 610000 :18411
: Method_3:
e, 610000 :10968
: Method_replace_RegExp_Function:
e, 610000 :1651
: Method_normal:
e, 610000 :12339
まとめ:正規表現の強力な検索機能を盲信することはできません.正則のマッチング過程は一回の循環です.だから正則のマッチングは多すぎてはいけません.Stering.replaceを上手に使うのが効率的です.JavaScript replace(RegExp,Function)詳細