在这种情况下,String.Format 或 String.Replace 哪个更快?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为什么要重新发明 String.Format?
我只是使用框架方法 - 它完全符合您的要求,很强大,并且对后续的内容有意义......
只是为了满足您的好奇心:
String.Format,仅供参考,在内部使用 StringBuilder.AppendFormat。话虽如此,StringBuilder.AppendFormat 非常高效。您提到它会“逐个字符”地遍历字符串 - 但是,在您的情况下,您使用的是对 Replace 和 Concat 的多次调用。使用 StringBuilder 一次传递字符串可能会快得多。如果您确实需要知道 - 您可以对此进行分析以进行检查。在我的机器上,如果我运行上述 1,000,000 次,我会得到以下计时:
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:
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:
自定义过程将增加每个附加占位符的成本,并在每次中间调用
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 toReplace
,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.如果您只想连接一些字符串,为什么不这样做呢?
或者
If all you want is to concatenate some strings, why not just do that?
or
在 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.