是的String.Format尽可能高效的StringBuilder [英] Is String.Format as efficient as StringBuilder

查看:111
本文介绍了是的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.

在这两种情况下,上面我想注入一个或多个字符串连接为pdefined模板字符串一个$ P $的中间。

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

很抱歉的混乱

推荐答案

的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();
}

以上code是mscorlib程序片段,所以问题就变成了是 StringBuilder.Append() StringBuilder.AppendFormat快()

如果没有标杆我可能会说,上面的code样品将运行得更快使用 .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.

这家伙,杰里迪克森,做了一些基准测试:

This chap, Jerry Dixon, did some benchmarking:

<一个href=\"http://jdixon.dotnetdevelopersjournal.com/string_concatenation_stringbuilder_and_stringformat.htm\">http://jdixon.dotnetdevelopersjournal.com/string_concatenation_stringbuilder_and_stringformat.htm

更新:

不幸的是上面的链接已经去世。但是仍然有回来的路上机副本:

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

<一个href=\"http://web.archive.org/web/20090417100252/http://jdixon.dotnetdevelopersjournal.com/string_concatenation_stringbuilder_and_stringformat.htm\">http://web.archive.org/web/20090417100252/http://jdixon.dotnetdevelopersjournal.com/string_concatenation_stringbuilder_and_stringformat.htm

目前这取决于你的字符串格式化是否将被反复叫天结束时,即你正在做一些严重的文本处理过的文字兆100的,还是当用户现在点击一个按钮,它被称为然后再次。除非你正在做一些巨大的批处理工作我会用的String.Format坚持下去,它有助于code可读性。如果怀疑有PERF的瓶颈,然后坚持你的code探查器,看看它到底是什么。

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天全站免登陆