在这种情况下,String.Format 或 String.Replace 哪个更快?

发布于 2024-09-25 06:37:47 字数 601 浏览 1 评论 0原文

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

我应该执行以下操作,

str = string.Format(str, "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;
}

看来 StringBuilder.AppendFormat() 不是在进行像这样的简单替换时非常高效,因为它会逐个字符地遍历字符串。

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");

 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

森林散布 2024-10-02 06:37:48

为什么要重新发明 String.Format?

我只是使用框架方法 - 它完全符合您的要求,很强大,并且对后续的内容有意义......


只是为了满足您的好奇心:

在进行像这样的简单替换时,StringBuilder.AppendFormat() 似乎效率不高,因为它逐个字符地遍历字符串。

String.Format,仅供参考,在内部使用 StringBuilder.AppendFormat。话虽如此,StringBuilder.AppendFormat 非常高效。您提到它会“逐个字符”地遍历字符串 - 但是,在您的情况下,您使用的是对 Replace 和 Concat 的多次调用。使用 StringBuilder 一次传递字符串可能会快得多。如果您确实需要知道 - 您可以对此进行分析以进行检查。在我的机器上,如果我运行上述 1,000,000 次,我会得到以下计时:

String.Format -  1029464 ticks
Custom method -  2988568 ticks

Why do you want to reinvent 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.

String.Format, FYI, uses StringBuilder.AppendFormat internally. That being said, StringBuilder.AppendFormat is quite efficient. 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. A single pass through the string with a StringBuilder is likely to be much quicker. 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:

String.Format -  1029464 ticks
Custom method -  2988568 ticks
握住我的手 2024-10-02 06:37:48

自定义过程将增加每个附加占位符的成本,并在每次中间调用 Replace 时为垃圾收集器生成一次性字符串。

除了 string.Format 可能比多次调用 Replace 快得多之外,string.Format 还包含对区域性敏感操作的重载。

string.Format 的灵活性和直观性至少与速度一样引人注目。

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.

Besides the likelihood that string.Format is much faster than multiple calls to Replace, string.Format includes overloads to culture-sensitive operations as well.

The flexibility and intuitiveness of string.Format is at least as compelling as the speed.

屋檐 2024-10-02 06:37:48

如果您只想连接一些字符串,为什么不这样做呢?

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

或者

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

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");
汐鸠 2024-10-02 06:37:48

在 C# 中,+ 运算符实际上转换为 string.Concat(),我一直认为 String.Format("Hello: {0} {1}", "Bob", "Jones")是最快的。事实证明,在发布模式下启动调试器外部运行的示例后,"Bob" + "Jones" 速度要快得多。但有一个截止点。我相信大约 8 个串联左右, string.Format 会变得更快。

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. It turns out, after firing up a sample ran outside the debugger, in release mode, that "Bob" + "Jones" is much faster. There is a cutoff point though. I believe around 8 concatenations or so, string.Format becomes faster.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文