哪种方法更好 [英] Which method is better

查看:85
本文介绍了哪种方法更好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪种方法最好从字符串中删除字符?


替换或子字符串?


考虑到第一个字符是在0位置。使用反射器,替换让

在内部调用中失去了它的轨迹,而子串显示它真正的作用,

所以我无法评估效率。 />

有什么建议吗?


谢谢

Andrea

Which method is better to remove a char from a string?

A replace or a substring?

Considering that first char is at 0 position. Using reflector, replace let
lost its track in internal call, while substring show what it really does,
so I''m not able to evaluate the efficiency.

Any suggestion?

Thanks
Andrea

推荐答案

哪种方法最好从字符串中删除字符?
Which method is better to remove a char from a string?

>

替换或子串?


考虑到第一个字符是0位置。使用反射器,替换

让它在内部调用中失去轨道,而子串显示它实际上是什么,所以我无法评估效率。 />

有什么建议吗?
>
A replace or a substring?

Considering that first char is at 0 position. Using reflector, replace
let lost its track in internal call, while substring show what it
really does, so I''m not able to evaluate the efficiency.

Any suggestion?



当你在Reflector中看到一个[MethodImpl(MethodImplOptions.InternalCall]属性

a .NET框架方法时,这意味着该方法在运行时本身内实现了
- 通常是为了使方法更快或访问

一些内部数据结构。如果你想看看这些方法是如何可能的那样

已实施,请从 http:// msdn。中查看共享源CLI。 microsoft.com/net/sscli


String.Replace(Char,Char):字符串在sscli \ clr \ src \ vm \ comstring中实现.cpp \ COMString :: Replace

String.Replace(String,String):字符串在sscli\clr\src\vm\comstring.cpp \ COMString :: ReplaceS中实现tring


看看这个来源,虽然很有趣,但实际上并没有给你一点感觉

的效率。首先,它不是ñ .NET框架是如何在幕后实现商业发行的。其次,比较

un-JITted托管代码与本机代码相比是徒劳的。


测试效率的一个好方法是创建一个小型测试平台每个方法多次执行
并比较时间。如果您使用的是.NET

2.0,则可以使用System.Diagnostics.Stopwatch为方法计算时间,确保精确度为
。一定要采取几个样本并平均时间,因此你可以获得更准确的读数,因为在你的

系统上运行的其他进程和垃圾收集器可能会干扰你的基准测试。


最诚挚的问候,

Dustin Campbell

Developer Express Inc.

When you see an [MethodImpl(MethodImplOptions.InternalCall] attribute on
a .NET framework method in the Reflector, that means that the method is implemented
inside the runtime itself -- usually to make the method faster or access
some internal data structure. If you want to see how these methods are likely
implemented, check out the Shared Source CLI from http://msdn.microsoft.com/net/sscli.

String.Replace(Char, Char) : String is implemented at sscli\clr\src\vm\comstring.cpp\COMString::Replace
String.Replace(String, String) : String is implemented at sscli\clr\src\vm\comstring.cpp\COMString::ReplaceS tring

Looking at this source, while interesting, won''t really give you a sense
of efficiency. First of all, it''s not necessarily how the .NET framework
commercial distribution is implemented under-the-hood. And secondly, comparing
un-JITted managed code against native code is pretty futile.

A good way to test efficiency is to create a small test bench that executes
each method several times and compares the timings. If you are using .NET
2.0, you can use the System.Diagnostics.Stopwatch to time the methods with
fair accuracy. Be sure to take several samples and average the timings so
that you can get more accurate readings as other processes running on your
system and the garbage collector may intrude on your benchmarking.

Best Regards,
Dustin Campbell
Developer Express Inc.


Andrea< go ********** @ spam.no.cleanmail.itwrote:
Andrea <go**********@spam.no.cleanmail.itwrote:

哪种方法更适合删除char从字符串?


替换或子串?
Which method is better to remove a char from a string?

A replace or a substring?



替换不会删除它 - 它将替换它。所以从

正确性的角度来看,你想要子串。


-

Jon Skeet - < sk * **@pobox.com>
http://www.pobox.com/ ~Sibet 博客: http://www.msmvps.com/jon。双向飞碟

如果回复小组,请不要给我发邮件

Replace won''t remove it - it will replace it. So just from a
correctness point of view, you want Substring.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Andrea,


这取决于你想要删除一个角色的方式。如果你想

删除一个或多个特定字符的出现,那么替换

将是你最好的选择。如果你想在

特定位置删除一个字符,那么Substring可能是更好的选择。


Brian


Andrea写道:
Andrea,

It depends on how you want to remove a character. If you want to
remove one or more occurrences of a specific character then Replace
would be your best bet. If you want to remove a character at a
specific position then Substring might be a better option.

Brian

Andrea wrote:

哪种方法最好从字符串中删除字符?


替换或子字符串?


考虑到第一个字符位于0位置。使用反射器,替换让

在内部调用中失去了它的轨迹,而子串显示它真正的作用,

所以我无法评估效率。 />

有什么建议吗?


谢谢

Andrea
Which method is better to remove a char from a string?

A replace or a substring?

Considering that first char is at 0 position. Using reflector, replace let
lost its track in internal call, while substring show what it really does,
so I''m not able to evaluate the efficiency.

Any suggestion?

Thanks
Andrea

这篇关于哪种方法更好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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