作者在 2009-10-25 10:50:04 发布以下内容
C#字符串拼接 "+"连接与Append()方法 速度比较
闲来无事,为了验证一下 "+"与append()方法拼接字符串速度比较,append方法更快,究竟快多少,在什么样的数量级别用"+"连接,在什么样的级别要改用append()方法连接字符串,写了一段代码,一测试,结果有点出乎意外。
拼交接例子就是连接字符串1,结果发现在拼接10 000个1,两者速度不相上下,但到了100 000个级别,“+”连接就显得很无力了,居然要了将近40s,很明显,时间与数量并非线性递增关系,然页append()方法依然超快,几乎不要时间 (62毫秒)。再测试,在40 000级别,“+”连接需时就达1秒左右,append()在10 000 000(千万级),也只要了750毫秒,只是在页面输出时没有能显示完,挂机了。“+”百万级没敢做了,估计当机。
大量字符串拼接时,append()方法明显优于"+"连接。
测试环境: T5600 2G DDR2 IE8 VS2008 (同时45个进程)
前台: “+”连接 ADD测试 append () 方法 APP测试
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="conbineStringTest.aspx.cs" Inherits="test_conbineStringTest" %>
- <!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 runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- 请输入长度 <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
- <asp:Button ID="Button2" runat="server" Text="ADD测试"
- onclick="Button2_Click" />
- <asp:Button ID="Button3" runat="server" Text="APP测试"
- onclick="Button3_Click" /><br/>
- <span id="add" runat="server"></span><br/>
- <span id="app" runat="server"></span>
- </div>
- </form>
- </body>
- </html>
后台:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Text;
- public partial class test_conbineStringTest : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- protected void Button2_Click(object sender, EventArgs e)
- {
- clear();
- int i = 0;
- if (System.Text.RegularExpressions.Regex.IsMatch(this.TextBox1.Text, @"\d+"))
- {
- int count = Convert.ToInt32(this.TextBox1.Text);
- DateTime startTime = DateTime.Now;
- string r = null;
- for (i = 1; i <= count; i++)
- {
- r += "1";
- if (i % 100 == 0)
- {
- r += "<br/>";
- }
- }
- DateTime endTime = DateTime.Now;
- this.add.InnerHtml = "ADD开始于:" + startTime.ToString("yyyy-MM-dd HH:mm:ss.fff") + " 结束于:" + endTime.ToString("yyyy-MM-dd HH:mm:ss.fff") + "用时:" + DateDiff(endTime, startTime) + "<br/>" + r;
- }
- else
- {
- this.add.InnerHtml = "请输入一个数字";
- }
- }
- private string DateDiff(DateTime DateTime1, DateTime DateTime2)
- {
- string dateDiff = null;
- TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
- TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
- TimeSpan ts = ts1.Subtract(ts2).Duration();
- //dateDiff = ts.Days.ToString() + "天"
- // + ts.Hours.ToString() + "小时"
- dateDiff = ts.Minutes.ToString() + "分钟"
- + ts.Seconds.ToString() + "秒"
- + ts.Milliseconds.ToString() + "毫秒";
- return dateDiff;
- }
- protected void Button3_Click(object sender, EventArgs e)
- {
- clear();
- int i = 0;
- int count = Convert.ToInt32(this.TextBox1.Text);
- DateTime startTime = DateTime.Now;
- StringBuilder sb = new StringBuilder();
- for (i = 1; i <= count; i++)
- {
- sb.Append("1");
- if (i % 100 == 0)
- {
- sb.Append("<br>");
- }
- }
- DateTime endTime = DateTime.Now;
- this.app.InnerHtml = "APP开始于:" + startTime.ToString("yyyy-MM-dd HH:mm:ss.fff") + " 结束于:" + endTime.ToString("yyyy-MM-dd HH:mm:ss.fff") + "用时:" + DateDiff(endTime, startTime) + "<br/>" + sb.ToString();
- }
- protected void clear()
- {
- this.app.InnerHtml = null;
- this.add.InnerHtml = null;
- }
- }