C#字符串连接消耗
来源:互联网 发布:淘宝客推广平台有哪些 编辑:程序博客网 时间:2024/06/05 07:47
1、Concatenation is the process of appending one string to the end of another string. When you concatenate string literals or string constants by using the +
operator, the compiler creates a single string. No run time concatenation occurs. However, string variables can be concatenated only at run time. In this case, you should understand the performance implications of the various approaches.
2、If you are not concatenating large numbers of strings (for example, in a loop), the performance cost of this code is probably not significant. The same is true for the String.Concat and String.Format methods.
However, when performance is important, you should always use the StringBuilder class to concatenate strings.
3、
This is the most interesting test because we have several options here. We can concatenate strings with +, String.Concat
, String.Join
and StringBuilder.Append
.
string Add(params string[] strings) // Used Test functions for this chart{ string ret = String.Empty; foreach (string str in strings) ret += str; return ret;}string Concat(params string[] strings){ return String.Concat(strings);}string StringBuilderAppend(params string[] strings){ StringBuilder sb = new StringBuilder(); foreach (string str in strings) sb.Append(str); return sb.ToString();}string Join(params string[] strings){ return String.Join(String.Empty, strings);}
And the winner for String Concatenation is ... Not string builder but String.Join
? After taking a deep look with Reflector I found that String.Join has the most efficient algorithm implemented which allocates in the first pass the final buffer size and then memcopy each string into the just allocated buffer. This is simply unbeatable. StringBuilder
does become better above 7 strings compared to the + operator but this is not really code one would see very often.
参考:
1、https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/strings/how-to-concatenate-multiple-strings
2、https://www.codeproject.com/Articles/14936/StringBuilder-vs-String-Fast-String-Operations-wit
- C#字符串连接消耗
- C#连接字符串收集
- C#连接Oracle 字符串
- C#连接SQLite字符串
- C# 各种连接字符串
- C#各种连接字符串
- C# SQLite连接字符串
- C#连接数据库连接字符串
- C# 字符串的连接
- C#生产者消耗者
- c#缓存机制,用于大批量连接数据库的性能优化,可减少对数据库的消耗。
- c# 连接oracle数据库字符串
- c#字符串连接性能测试
- C#版mysql连接字符串
- C#版mysql连接字符串
- c#连接SQLite的字符串
- C#中Access 连接字符串
- C#的oracle连接字符串
- 十月培训
- Android学习《第一行代码》01
- 锁小结
- RecyclerView的基本使用
- BFS和DFS解决LeetCode133. Clone Graph
- C#字符串连接消耗
- JDBC连接 防止注入 事件 回滚
- hibernate setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP)
- 佛祖保佑 永无BUG
- jquery几种加载方式
- [Machine Learning & Algorithm] 随机森林(Random Forest)
- java 2048游戏设计初体验
- PAT乙级1031. 查验身份证(15)
- sourceInsight4 --- 破解