String.Format 和 StringBuilder 一样有效吗 [英] Is String.Format as efficient as StringBuilder

查看:42
本文介绍了String.Format 和 StringBuilder 一样有效吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在 C# 中有一个 stringbuilder 来执行此操作:

Suppose I have a stringbuilder in C# that does this:

StringBuilder sb = new StringBuilder();
string cat = "cat";
sb.Append("the ").Append(cat).(" in the hat");
string s = sb.ToString();

这是否与拥有一样有效或更有效:

would that be as efficient or any more efficient as having:

string cat = "cat";
string s = String.Format("The {0} in the hat", cat);

如果是,为什么?

编辑

经过一些有趣的回答后,我意识到我可能应该更清楚我的问题.我并不是要问哪个在连接字符串时更快,而是在一个字符串注入另一个字符串时哪个更快.

After some interesting answers, I realised I probably should have been a little clearer in what I was asking. I wasn't so much asking for which was quicker at concatenating a string, but which is quicker at injecting one string into another.

在上述两种情况下,我都想将一个或多个字符串注入到预定义模板字符串的中间.

In both cases above I want to inject one or more strings into the middle of a predefined template string.

抱歉打扰了

推荐答案

注意: 此答案是在 .NET 2.0 是当前版本时编写的.这可能不再适用于更高版本.

NOTE: This answer was written when .NET 2.0 was the current version. This may no longer apply to later versions.

String.Format 在内部使用 StringBuilder:

public static string Format(IFormatProvider provider, string format, params object[] args)
{
    if ((format == null) || (args == null))
    {
        throw new ArgumentNullException((format == null) ? "format" : "args");
    }

    StringBuilder builder = new StringBuilder(format.Length + (args.Length * 8));
    builder.AppendFormat(provider, format, args);
    return builder.ToString();
}

上面的代码是 mscorlib 的一个片段,所以问题变成了StringBuilder.Append() 是否比 StringBuilder.AppendFormat() 快"?

The above code is a snippet from mscorlib, so the question becomes "is StringBuilder.Append() faster than StringBuilder.AppendFormat()"?

如果没有基准测试,我可能会说上面的代码示例使用 .Append() 会运行得更快.但这是一个猜测,请尝试对两者进行基准测试和/或分析以进行适当的比较.

Without benchmarking I'd probably say that the code sample above would run more quickly using .Append(). But it's a guess, try benchmarking and/or profiling the two to get a proper comparison.

这个家伙 Jerry Dixon 做了一些基准测试:

This chap, Jerry Dixon, did some benchmarking:

http://jdixon.dotnetdevelopersjournal.com/string_concatenation_stringbuilder_and_stringformat.htm

更新:

遗憾的是,上面的链接已经失效.然而,Way Back Machine 上仍有一份副本:

Sadly the link above has since died. However there's still a copy on the Way Back Machine:

http://web.archive.org/web/20090417100252/http://jdixon.dotnetdevelopersjournal.com/string_concatenation_stringbuilder_and_stringformat.htm

归根结底,这取决于您的字符串格式是否会被重复调用,即您正在对 100 兆字节的文本进行一些严肃的文本处理,或者是否在用户单击按钮时调用它然后再次.除非你正在做一些巨大的批处理工作,否则我会坚持使用 String.Format,它有助于代码可读性.如果您怀疑存在性能瓶颈,请在您的代码上贴上分析器,看看它到底在哪里.

At the end of the day it depends whether your string formatting is going to be called repetitively, i.e. you're doing some serious text processing over 100's of megabytes of text, or whether it's being called when a user clicks a button now and again. Unless you're doing some huge batch processing job I'd stick with String.Format, it aids code readability. If you suspect a perf bottleneck then stick a profiler on your code and see where it really is.

这篇关于String.Format 和 StringBuilder 一样有效吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆