几十到上百k字符串输出,用
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Write("一部分一部分");
HttpContext.Current.Response.End();还是
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Write("用StringBuilder累加后的全部字符");
HttpContext.Current.Response.End();
哪个效率高,请细说,非常感谢!!!
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Write("一部分一部分");
HttpContext.Current.Response.End();还是
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Write("用StringBuilder累加后的全部字符");
HttpContext.Current.Response.End();
哪个效率高,请细说,非常感谢!!!
你输出的方式并没有变化只是比字符串+号拼接和StringBuilder哪个好。。用StringBuilder
Stopwatch watch=new Stopwatch();
watch.Start();
.....
watch.Stop();
Response.Write(watch.ElapsedMilliseconds+"毫秒");
LZ的方法需要比对的是
HttpContext.Current.Response.Write()和StringBuilder.Append()这两个方法做字符串串联效率,而不是 +运算 和 StringBuilder.Append()的效率比对。 曾在stackoverflow看过HttpContext.Current.Response.Write()串联效率的说明,其比+运算高出很多,其将字符流写入到一个缓冲区,最后再一次性输出来,原理和StringBuilder异曲同工,但HttpContext.Current.Response.Write()个StringBuilder.Append()的效率高低我无法直接给出结论,现在手头没有工具测试,建议LZ写个测试代码测试一下。
如果HttpContext.Current.Response.Write()和StringBuilder.Append()效率几乎一样,那么无疑用HttpContext.Current.Response.Write(),因为StringBuilder的创建到最后ToString的过程有额外的开销。