找到一篇国外的代码,专门来测试这个,

String+

String.Concat

String.Format

StringBuilder

前三个在100个左右字符串差不多,

String.Concat会获得稍微好一点点的性能提高,

String.Format会让你使用起来更方便,

StringBuilder更适合更多更长的字符串拼接,

如果有其它见解,还请指导。

using System;
using System.Diagnostics;
using System.Text;
namespace CompareInstructionExecutionSpeed
{
public delegate void CompareExcecutionSpeed(int loop);
class Program
{
public static string ResultConcatenation = string.Empty;
public static readonly StringBuilder Sb = new StringBuilder();
public static readonly Stopwatch Stopwatch = new Stopwatch(); public static void Main()
{
CompareExcecutionSpeed methods = StringBuilderExecutionSpeed;
methods += StringConcatExecutionSpeed;
methods += ManualConcatenationExecutionSpeed;
methods += StringFormatExecutionSpeed;
//methods+=Some Method -- you can add your method to calculate speed. methods.Invoke();//count Console.ReadKey();
} //Elapsing StringBuilder -------------------------------------------
public static void StringBuilderExecutionSpeed(int loop)
{
Stopwatch.Restart();
for (int i = ; i < loop; i++)
{
ShowPercentProgress(i, loop);
Sb.Append(" str");
Sb.AppendLine(i.ToString());
}
Stopwatch.Stop();
ShowCompareResult("StringBuilder", Stopwatch);
} //Elapsing Str1+Str2+... -------------------------------------------
public static void ManualConcatenationExecutionSpeed(int loop)
{
Stopwatch.Restart();
for (int i = ; i < loop; i++)
{
ShowPercentProgress(i, loop);
ResultConcatenation += " str" + i + "\n";
}
Stopwatch.Stop();
ShowCompareResult("str1+str2+...", Stopwatch);
} //Elapsing String.Concat -------------------------------------------
public static void StringConcatExecutionSpeed(int loop)
{
Stopwatch.Restart();
for (int i = ; i < loop; i++)
{
ShowPercentProgress(i, loop);
ResultConcatenation += string.Concat(" str", i, "\n");
}
Stopwatch.Stop();
ShowCompareResult("String.Concat", Stopwatch); } //Elapsing String.Format -------------------------------------------
public static void StringFormatExecutionSpeed(int loop)
{
Stopwatch.Restart();
for (int i = ; i < loop; i++)
{
ShowPercentProgress(i, loop);
ResultConcatenation += string.Format(" str{0}\n", i);
}
Stopwatch.Stop();
ShowCompareResult("String.Format", Stopwatch);
} //Show Compare Result---------------------------------------------
public static void ShowCompareResult(string message, Stopwatch stopwatch)
{
Console.ResetColor();
Console.WriteLine("\r{0}\t{1,9} Millisecond ~={2,3} second ~={3,3} minutes",
message,
Math.Round(stopwatch.Elapsed.TotalMilliseconds),
Math.Round(stopwatch.Elapsed.TotalSeconds),
Math.Round(stopwatch.Elapsed.TotalMinutes));
} //Show processing progress----------------------------------------
static void ShowPercentProgress(int currElementIndex, int totalElementCount)
{
Console.ForegroundColor = ConsoleColor.Green;
int percent = ( * (currElementIndex + )) / totalElementCount;
Console.Write("\r{0}%", percent);
}
}
}

String+ String.Concat String.Format StringBuilder 之间的性能测试的更多相关文章

  1. String、StringBuffer与StringBuilder之间区别[全屏看文]

    String.StringBuffer与StringBuilder之间区别[全屏看文]   最近学习到StringBuffer,心中有好些疑问,搜索了一些关于String,StringBuffer,S ...

  2. String、StringBuffer、StringBuilder之间区别

    String,StringBuffer,StringBuilder 之间区别 在字符串处理中C#提供了String.StringBuffer.StringBuilder三个类.那么他们到底有什么优缺点 ...

  3. String、StringBuffer与StringBuilder之间区别(转)

    原文链接:String.StringBuffer与StringBuilder之间区别 最近学习到StringBuffer,心中有好些疑问,搜索了一些关于String,StringBuffer,Stri ...

  4. 自己(转)String、StringBuffer与StringBuilder之间区别

    String.StringBuffer与StringBuilder之间区别   最近学习到StringBuffer,心中有好些疑问,搜索了一些关于String,StringBuffer,StringB ...

  5. (转)String、StringBuffer与StringBuilder之间区别

    原文地址: http://www.cnblogs.com/A_ming/archive/2010/04/13/1711395.html 关于这三个类在字符串处理中的位置不言而喻,那么他们到底有什么优缺 ...

  6. String、StringBuffer、StringBuilder之间的区别

    String                      字符串常量 StringBuffer         字符串变量(线程安全) StringBuilder       字符串变量(非线程安全) ...

  7. java中String,StringBuffer,StringBuilder之间的区别

    文章转载自:http://www.cnblogs.com/frankliiu-java/archive/2010/07/05/1771537.html String是固定长度的字符串,如果要发生变化必 ...

  8. Java中String、StringBuffer和StringBuilder之间的区别

    String在Java中是字符串常量 例如 String str = "abc"; str = str + 1; System.out.println(str); 结果将是abc1 ...

  9. String、Stringbuffer和Stringbuilder之间的区别

    关于这三个类在字符串处理中的位置不言而喻,那么他们到底有什么优缺点,到底什么时候该用谁呢?下面我们从以下几点说明一下 1.在执行速度方面:Stringbuilder>Stringbuffer&g ...

随机推荐

  1. Android开发——xml知识之xml可绘制对象简介

    引言:摘录Android用户界面设计 Android支持各类xml可绘制对象,其中一些可绘制对象可用于实现让一个资源使用多个图片文件,另外一些则允许你在xml中真正地指定颜色,还有一些你从来都不会用到 ...

  2. Jenkins关于tomcat地址和端口映射的配置

    <?xml version='1.0' encoding='utf-8'?><!-- Licensed to the Apache Software Foundation (ASF) ...

  3. linux c编程:初识进程与线程

    p { margin-bottom: 0.25cm; line-height: 120% } (一) 认识进程 在Linux系统中,每一个进程都有自己的ID,就如同人的身份证一样.linux中有一个数 ...

  4. C# 文件的操作

    C#对文件的操作相当方便,主要涉及到四个类:File.FileInfo.Directory.DirectoryInfo,前两个提供了针对文件的操作,后两个提供了针对目录的操作,类图关系如上图所示; 下 ...

  5. 转:【Java并发编程】之二:线程中断(含代码)

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/17091267 使用interrupt()中断线程 当一个线程运行时,另一个线程可以调用对应 ...

  6. 【2017集美大学1412软工实践_助教博客】团队作业9——测试与发布(Beta版本)

    题目 团队作业9--测试与发布(Beta版本)(http://www.cnblogs.com/happyzm/p/6917253.html) 团队作业9-1 测试与发布成绩 分值 1 0.5 0.5 ...

  7. 软件工程——构建之法高分Tips

    不想获得高分的学生不是好程序猿,结合助教的经验,要想在这门课程上获得高分先提几个Tips 仔细阅读作业要求,尽可能完成作业的每个点 每次老师作业要求布置的都很详细,想获得高分的同学应该仔细阅读作业要求 ...

  8. 201521123016 《Java程序设计》第8周学习总结

    1. 本周学习总结 2. 书面作业 本次作业题集集合 1.List中指定元素的删除(题目4-1) 1.1 实验总结 1.删除元素的时候从最后一个元素开始,避免删除元素后位置发生变化而导致有些元素没有删 ...

  9. 201521123105 第三周Java学习总结

    1. 本周学习总结 对象(实际个体) 对象与类 类(模板) 2.书面作业 1.代码阅读 public class Test1 { private int i = 1;//这行不能修改 private ...

  10. evak购物车--课程设计(201521123037邱晓娴)

    1. 团队课程设计博客链接 团队博客 2. 个人负责模块或任务说明 1.Java (1)编写用户类Users (2)编写DBConnection类,连接数据库 (3)编写GoodsDAO类,从数据库中 ...