javascript中字符串连接时用Array.join()替换 string += "xx",换来几十倍的速度提升
来源:互联网 发布:编程难么 编辑:程序博客网 时间:2024/05/22 13:03
2006-12-31 21:38 by 无常, 4582 visits, 网摘, 收藏, 编辑
下面的二个函数compute1()和compute1(),都是将50000个字符串连接起来,
直接用+=连接耗时17547毫秒,
使用Array.join()耗时234毫秒,比前者快了近75倍!
而且使用+=操作的话,随着循环次数的增加,耗用时间是nn倍的上升,循环30000次时近60秒,
而用Array.join循环50000次才是843毫秒。
javascript的string是固定内存的,每次对字符串的修改操作都会导致重新分配内存,速度当然慢了。
c#中的string也是固定分配内存的,所以在做多字符串连接时一定要记得StringBuilder哦.
直接用+=连接耗时17547毫秒,
使用Array.join()耗时234毫秒,比前者快了近75倍!
而且使用+=操作的话,随着循环次数的增加,耗用时间是nn倍的上升,循环30000次时近60秒,
而用Array.join循环50000次才是843毫秒。
javascript的string是固定内存的,每次对字符串的修改操作都会导致重新分配内存,速度当然慢了。
c#中的string也是固定分配内存的,所以在做多字符串连接时一定要记得StringBuilder哦.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Untitled Page</title>
</head>
<body>
<input id="Button1" type="button" value="直接连接" onclick="compute1();"/>
开始时间:<input id="Text1" type="text" /> 结束时间:<input id="Text2" type="text" />
耗时:<input id="Text3" type="text" /><br />
<br />
<input id="Button2" type="button" value="StringBuilder" onclick="compute2();"/>开始时间:<input id="Text4" type="text" />结束时间:<input id="Text5" type="text" />
耗时:
<input id="Text6" type="text" />
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Untitled Page</title>
</head>
<body>
<input id="Button1" type="button" value="直接连接" onclick="compute1();"/>
开始时间:<input id="Text1" type="text" /> 结束时间:<input id="Text2" type="text" />
耗时:<input id="Text3" type="text" /><br />
<br />
<input id="Button2" type="button" value="StringBuilder" onclick="compute2();"/>开始时间:<input id="Text4" type="text" />结束时间:<input id="Text5" type="text" />
耗时:
<input id="Text6" type="text" />
</body>
</html>
<script language=javascript>
function compute1()
{
var start = new Date();
window.document.all.Text1.value =start.getSeconds() * 1000 + start.getMilliseconds();
str = "";
for( i=0; i<20000; i++)
{
str += "wuchang@guet.edu.cn";
}
var end = new Date();
window.document.all.Text2.value =end.getSeconds() * 1000 + end.getMilliseconds();
window.document.all.Text3.value = Number(window.document.all.Text2.value) -Number(window.document.all.Text1.value);
}
function compute2()
{
var start = new Date();
window.document.all.Text4.value =start.getSeconds() * 1000 + start.getMilliseconds();
str = new StringBuilder();
for( i=0; i<200000; i++)
{
str.Append("wuchang@guet.edu.cn");
}
var end = new Date();
window.document.all.Text5.value =end.getSeconds() * 1000 + end.getMilliseconds();
window.document.all.Text6.value = Number(window.document.all.Text5.value) -Number(window.document.all.Text4.value);
}
function StringBuilder(str)
{
this.tmp = new Array();
}
StringBuilder.prototype.Append= function(value)
{
this.tmp.push(value);
return this;
}
StringBuilder.prototype.Clear = function()
{
tmp.length=1;
}
StringBuilder.prototype.toString = function()
{
return this.tmp.join('');
}
</script>
function compute1()
{
var start = new Date();
window.document.all.Text1.value =start.getSeconds() * 1000 + start.getMilliseconds();
str = "";
for( i=0; i<20000; i++)
{
str += "wuchang@guet.edu.cn";
}
var end = new Date();
window.document.all.Text2.value =end.getSeconds() * 1000 + end.getMilliseconds();
window.document.all.Text3.value = Number(window.document.all.Text2.value) -Number(window.document.all.Text1.value);
}
function compute2()
{
var start = new Date();
window.document.all.Text4.value =start.getSeconds() * 1000 + start.getMilliseconds();
str = new StringBuilder();
for( i=0; i<200000; i++)
{
str.Append("wuchang@guet.edu.cn");
}
var end = new Date();
window.document.all.Text5.value =end.getSeconds() * 1000 + end.getMilliseconds();
window.document.all.Text6.value = Number(window.document.all.Text5.value) -Number(window.document.all.Text4.value);
}
function StringBuilder(str)
{
this.tmp = new Array();
}
StringBuilder.prototype.Append= function(value)
{
this.tmp.push(value);
return this;
}
StringBuilder.prototype.Clear = function()
{
tmp.length=1;
}
StringBuilder.prototype.toString = function()
{
return this.tmp.join('');
}
</script>
- javascript中字符串连接时用Array.join()替换 string += "xx",换来几十倍的速度提升
- Revvel如何将视频转码速度提升几十倍?
- javascript 的字符串连接 join更优吗?
- 通过非聚集索引让select count(*) from 的查询速度提高几十倍、甚至千倍
- 通过非聚集索引让select count(*) from 的查询速度提高几十倍、甚至千倍
- 通过非聚集索引让select count(*) from 的查询速度提高几十倍、甚至千倍
- 通过非聚集索引让select count(*) from 的查询速度提高几十倍、甚至千倍
- 通过非聚集索引让select count(*) from 的查询速度提高几十倍、甚至千倍
- 使Firefox速度提升4倍的密技
- MongoDB Map Reduce速度提升20倍的优化宝典
- MongoDB MapReduce速度提升20倍的优化宝典
- MongoDB MapReduce速度提升20倍的优化宝典
- MongoDB MapReduce速度提升20倍的优化宝典
- MongoDB MapReduce速度提升20倍的优化宝典
- 如何将你的 MySQL 查询速度提升 300 倍
- 如何将你的 MySQL 查询速度提升 300 倍
- 如何将你的 MySQL 查询速度提升 300 倍
- 如何将你的 MySQL 查询速度提升 300 倍
- java:replace和replaceAll的区别
- ubuntu的环境变量设置
- 关于就业问题,大家说说看法
- IE6下DIV高度显示的Bug
- 如何备份你的linux系统
- javascript中字符串连接时用Array.join()替换 string += "xx",换来几十倍的速度提升
- 有关游戏开发的几点体会
- 4月13日记
- Windows 不规则窗体
- 545455
- Windows xp下的Informix connect配置方法
- Java诺曼底 www.javanmd.com
- CSS设计教程 例1-1 背景图片
- Way on c & c++ 小记 [六] – Rule of Three, 复制控制