2011年7月15日23:34:18
效率PK —— 统计字符串中字符出现次数
原文见:javascript 统计哪个字符出现的次数最多–修正版

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 textarrStr.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('\n'));console.stacks = [];}}
}

我的方法,使用str.replace(RegExp,Function) 进行遍历
关于str.replace(RegExp, function)的用法,请参考我的上一篇随笔《JavaScript replace(RegExp, Function)详解》

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:\n出现次数最多的是" + 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:\n出现次数最多的是" + 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毫秒

总结:不能迷信正则表达式的强大搜索功能,正则的每一次匹配过程就是一次循环
所以正则的匹配不能用太多,善用String.replace(RegExp, Function)才是高效的选择。

http://www.cn09.com/thread-150-1-1.html

转载于:https://www.cnblogs.com/arliang/archive/2011/07/16/2108319.html

JavaScript效率PK——统计特定字符在字符串中出现的次数相关推荐

  1. c 语言 字符 查找,C 语言实例 - 查找字符在字符串中出现的次数

    C 语言实例 - 查找字符在字符串中出现的次数 C 语言实例 C 语言实例 查找字符在字符串中的起始位置(索引值从 开始). 实例 #include int main() { char str[], ...

  2. js 判断一个字符在字符串中出现的次数 - 代码篇

    js 判断一个字符在字符串中出现的次数 - 代码篇 判断:字符串string中有多少个数字0? var string='0,0,0,0,1,1,0,1,1,0,1,0,0,1,1,1,1,0,1,1, ...

  3. 正则-某字符在字符串中出现的次数、字母数字下划线常用正则表达式

    正则-某字符在字符串中出现的次数: string test ="AAABBBBCCFFFF ";   //计算后返回结果:A3B4C2F4 Regex reg = new Rege ...

  4. python计算字符在字符串中出现的次数

    python计算所有字符在字符串中各自出现的次数 1 from collections import Counter str='1212jisajikodsakdokoakso' counts=Cou ...

  5. java中判断一个字符在字符串中出现的次数

    源代码: //java中判断一个字符出现的次数//在下面字符串中查找有几个啊public static void testFindChar(){String str = "啊!我爱你中国!啊 ...

  6. js 判断一个字符在字符串中出现的次数

    <script type="text/javascript">var s='djh.doiwe.esd.d.ddd0sdd.d.'; var n=(s.split('. ...

  7. 计算字符在字符串中出现的次数

    创建测试用表: CREATE OR REPLACE VIEW v AS SELECT 'A,B,C' AS c FROM DUAL; 现在要求计算列c中","出现的次数,Oracl ...

  8. python统计字符在文件中出现的次数_python字符串中字符出现次数(python获取字符串个数)...

    今天要说的是Python的字符串函数还是很方便的,只需要调用 count() 方法即可. 最近,我在一个网站上看到了一个自然语言处理课程.我详细解释了一些方法的使用,用例很容易理解,所以我想通过我的博 ...

  9. 1. 编写函数 cal_numbers:参数为字符串,统计并返回该字符串中的英文字母、空格、数字和其它字符的个数

    1. 编写函数 cal_numbers:,参数为字符串,统计并返回该字符串中的英文字母.空格.数字和其它字符的个数,程序框架: def cal_numbers(strVal):计算 strVal 中, ...

最新文章

  1. AI芯片市场,到底是怎么一回事?
  2. 举例让抽象问题具体化:栈的压入、弹出序列
  3. string、wstring、cstring、 char、 tchar、int转换方法
  4. 怎么更改eclipse中tomcat的server location
  5. Silver Cow Party POJ - 3268(正反建图+dijkstra)
  6. mysql数据库字符集设置_查看和设置MySQL数据库字符集
  7. java 线程 内存分配内存_漫谈JAVA语言的内存分配
  8. imagesLoaded-检测图片是否正确加载的js插件
  9. 计算机英语讲课笔记(2020-6-23)
  10. matlab雷诺曲线,多重网格法求解雷诺方程的MATLAB编程 - 程序语言 - 小木虫 - 学术 科研 互动社区...
  11. BUAAOO P5-P7 Elevator Simulation
  12. GCC 原子操作中 Acquire/Release/Consume/Relaxed 内存模型
  13. 西门子Step7安装和入门初步
  14. matlab画三维图像(plot3,mesh,surf)
  15. 243.STAMP图形界面微生物组分析软件
  16. 三年级计算机上册期末测试题,三年级上册期末试卷
  17. lzg_ad:EWF原理详解
  18. 2023跨境出海指南:马来西亚网红营销白皮书
  19. html字体名称有哪些,css有哪些字体系列?
  20. 问道服务器系统,《问道》全部服务器将正式更新至V1.351新版

热门文章

  1. 青岛软控为机电软一体化企业的PLM建设树立样板
  2. markdown转微信公众号编辑器
  3. int... num什么意思?
  4. MyBatis超详细笔记
  5. 警惕消费陷阱和霸王条款:在携程网订的机票2070元退票费1560元
  6. Python爬取金山词霸每日一句,存储到MySQL中
  7. Revit空闲事件(Idling Event)增强和外部事件(External Event)
  8. 用户登录功能以及登录拦截
  9. 拦截QT关闭窗口的CloseEvent()解析
  10. 机器学习中的目标函数总结