简体   繁体   English

在这个上下文String.Format或String.Replace会更快?

[英]What would be faster in this context String.Format or String.Replace?

string str = 'my {0} long string {1} need formatting';

Should I be doing the following, 我应该做以下事情,

str = string.Format(str, "really", "doesn't");

or creating a method like so and calling str = str.ReplaceWithValues("really", "doesn't"); 或创建一个这样的方法并调用str = str.ReplaceWithValues("really", "doesn't");

 public string ReplaceWithValues(this string str, params object[] values) {
    string ret = str;
    for (int i = 0; i < values.Length; i++) {
        ret = str.Replace(string.Concat("{", i, "}"), values.ToString());
    }
    return ret;
}

It seems like StringBuilder.AppendFormat() isn't efficient when it comes to doing simple replacements like this since it goes character by character through the string. 看起来StringBuilder.AppendFormat()在执行像这样的简单替换时效率不高,因为它通过字符串逐个字符地进行。

Why do you want to reinvent String.Format? 你为什么要重新发明String.Format?

I'd just use the framework method - it does exactly what you want, is robust, and is going to make sense to those that follow... 我只是使用框架方法 - 它完全符合你的要求,是健壮的,并且对那些跟随的人有意义......


Just to satisfy your curiousity: 只是为了满足你的好奇心:

It seems like StringBuilder.AppendFormat() isn't efficient when it comes to doing simple replacements like this since it goes character by character through the string. 看起来StringBuilder.AppendFormat()在执行像这样的简单替换时效率不高,因为它通过字符串逐个字符地进行。

String.Format, FYI, uses StringBuilder.AppendFormat internally. String.Format,FYI,在内部使用StringBuilder.AppendFormat。 That being said, StringBuilder.AppendFormat is quite efficient. 话虽如此,StringBuilder.AppendFormat非常有效。 You mention that it goes "character by character" through the string - however, in your case, you're using multiple calls to Replace and Concat instead. 你提到它通过字符串“逐字符” - 但是,在你的情况下,你正在使用多次调用Replace和Concat。 A single pass through the string with a StringBuilder is likely to be much quicker. 使用StringBuilder单次传递字符串可能要快得多。 If you really need to know- you could profile this to check. 如果您真的需要知道 - 您可以对此进行分析以进行检查。 On my machine, I get the following timings if I run both of the above 1,000,000 times: 在我的机器上,如果我同时运行以上1,000,000次,我会得到以下时间:

String.Format -  1029464 ticks
Custom method -  2988568 ticks

The custom procedure will increase its cost with each additional placeholder and produce throwaway strings for the garbage collector with each intermediate call to Replace . 自定义过程将增加每个额外占位符的成本,并为每个中间调用Replace生成垃圾收集器的一次性字符串。

Besides the likelihood that string.Format is much faster than multiple calls to Replace , string.Format includes overloads to culture-sensitive operations as well. 除了string.Format比多次调用Replace速度快得多之外, string.Format还包括对文化敏感操作的重载。

The flexibility and intuitiveness of string.Format is at least as compelling as the speed. string.Format的灵活性和直观性至少与速度一样引人注目。

If all you want is to concatenate some strings, why not just do that? 如果您只想连接一些字符串,为什么不这样做呢?

string result = "my " + x + " long string " + y + " need formatting";

or 要么

string result = string.Concat("my ", x, " long string ", y, " need formatting");

In C# the + operator actually turns in to a string.Concat(), and I always thought String.Format("Hello: {0} {1}", "Bob", "Jones") was the fastest. 在C#中,+运算符实际上是一个string.Concat(),我一直认为String.Format("Hello: {0} {1}", "Bob", "Jones")是最快的。 It turns out, after firing up a sample ran outside the debugger, in release mode, that "Bob" + "Jones" is much faster. 事实证明,在调试器外部启动一个样本后,在发布模式下, "Bob" + "Jones"的速度要快得多。 There is a cutoff point though. 但是有一个截止点。 I believe around 8 concatenations or so, string.Format becomes faster. 我相信大约8个串联左右,string.Format变得更快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM