C# 测试对象类型的最有效方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Most Efficient Way to Test Object Type
提问by Yaakov Ellis
I have values stored as strings in a DataTable
where each value could really represent an int
, double
, or string
(they were all converted to strings during an import process from an external data source). I need to test and see what type each value really is.
我将值作为字符串存储在 a 中DataTable
,其中每个值都可以真正表示一个int
、double
、 或string
(它们都在从外部数据源导入过程中转换为字符串)。我需要测试并查看每个值的真正类型。
What is more efficient for the application (or is there no practical difference)?
什么对应用程序更有效(或者没有实际区别)?
- Try to convert to
int
(and thendouble
). If conversion works, the returntrue
. If an exception is thrown, returnfalse
. - Regular expressions designed to match the pattern of an
int
ordouble
- Some other method?
- 尝试转换为
int
(然后double
)。如果转换有效,则返回true
. 如果抛出异常,则返回false
。 - 正则表达式旨在匹配
int
或double
- 还有什么方法?
采纳答案by gil
Would use double.TryParse, it has performance benefits.
将使用 double.TryParse,它具有性能优势。
回答by Matt Dawdy
I'd personally use int.tryparse, then double.tryparse. Performance on those methods is quite fast. They both return a Boolean. If both fail then you have a string, per how you defined your data.
我个人会使用 int.tryparse,然后使用 double.tryparse。这些方法的性能非常快。它们都返回一个布尔值。如果两者都失败,那么根据您定义数据的方式,您将获得一个字符串。
回答by Mike Stone
I would say, don't worry so much about such micro performance. It is much better to just get something to work, and then make it as clear and concise and easy to read as possible. The worst thing you can do is sacrifice readability for an insignificant amount of performance.
我会说,不要太担心这样的微观性能。最好让一些东西工作起来,然后让它尽可能清晰、简洁和易于阅读。你能做的最糟糕的事情就是为了微不足道的性能而牺牲可读性。
In the end, the best way to deal with performance issues is to save them for when you have data that indicates there is an actual performance problem... otherwise you will spend a lot of time micro-optimizing and actually cause higher maintenance costs for later on.
最后,处理性能问题的最佳方法是在您有数据表明存在实际性能问题时保存它们……否则您将花费大量时间进行微优化,实际上会导致更高的维护成本稍后的。
If you find this parsing situation is really the bottleneck in your application, THEN is the time to try and figure out what the fastest way to solve the problem is. I think Jeff (and many others) have blogged about this sort of thing a lot.
如果您发现这种解析情况确实是您的应用程序中的瓶颈,那么是时候尝试找出解决问题的最快方法了。我认为杰夫(和许多其他人)已经写了很多关于这种事情的博客。
回答by Keith
You'll get different results for the different methods depending on whether you compile with optimisations on. You basically have a few options:
您将获得不同方法的不同结果,具体取决于您是否使用优化进行编译。你基本上有几个选择:
object o;
//checking with is
o is int
//check type
o.GetType() != typeof( int )
//cast and catch exception
try{ int j = (int) o; }
catch {}
//use the tryparse
int.TryParse( Convert.ToString( o ), out j )
You can easily set up a console app that tries each of these 10,000 times and returns durations for each (test when o is an int and when it's something else).
您可以轻松设置一个控制台应用程序,该应用程序会尝试这 10,000 次中的每一次并返回每次的持续时间(测试 o 何时为 int 以及何时为其他值)。
The try-catch
method is the quickest if the object does hold an int, and by far the slowest if it doesn't (even slower than GetType
). int.TryParse
is pretty quick if you have a string, but if you have an unknown object it's slower.
try-catch
如果对象确实持有 int 则该方法是最快的,如果不持有则该方法是最慢的(甚至比 慢GetType
)。 int.TryParse
如果你有一个字符串,它会很快,但如果你有一个未知的对象,它会更慢。
Interestingly, with .Net 3.5 and optimisations turned on the o is int
check takes the same time as try-catch
when o actually is an int. o is int
is only slightly slower if o actually is something else.
有趣的是,在 .Net 3.5 和优化打开时,o is int
检查花费的时间与try-catch
o 实际上是 int 时的时间相同。o is int
如果 o 实际上是其他东西,则只会稍微慢一点。
Annoyingly FxCop will throw up warnings if you do something like:
如果您执行以下操作,烦人的 FxCop 会发出警告:
if( o is int )
int j = (int) o;
But I think that's a bug in FxCop - it doesn't know int is a value type and recommends you to use o as int
instead.
但我认为这是 FxCop 中的一个错误 - 它不知道 int 是一种值类型,并建议您o as int
改用。
If your input is always a string int.TryParse
is best, otherwise the is
operator is quickest.
如果您的输入始终是字符串int.TryParse
最好,否则is
操作符是最快的。
As you have a string I'd look at whether you need to know that it's an int, rather than a double. If int.TryParse
passes then so will double.TryParse
so you could half the number of checks - return either double or string and floor the doubles when you expect an int.
当你有一个字符串时,我会看看你是否需要知道它是一个整数,而不是一个双精度数。如果int.TryParse
通过,那么double.TryParse
您可以将检查次数减半 - 返回 double 或 string 并在您期望 int 时将双打平铺。
回答by gods gift
The trouble you have is that there could be situations where the answer could be all three types.
您遇到的问题是,在某些情况下,答案可能是所有三种类型。
3 could be an int, a double or a string!
3 可以是 int、double 或 string!
It depends upon what you are trying to do and how important it is that they are a particular type. It might be best just to leave them as they are as long as you can or, alternatively, some up with a method to mark each one (if you have control of the source of the original string).
这取决于您要尝试做什么以及它们是特定类型的重要性。最好将它们保留尽可能长的时间,或者,使用一种方法来标记每个字符串(如果您可以控制原始字符串的来源)。