什么最快是:(INT),Convert.ToInt32(x)或Int32.Parse(x)的? [英] What is fastest: (int), Convert.ToInt32(x) or Int32.Parse(x)?

查看:261
本文介绍了什么最快是:(INT),Convert.ToInt32(x)或Int32.Parse(x)的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这下面的代码是最快的/最佳实践转化的一些对象x?



  INT敏=(INT)x ; 

  INT敏= Convert.ToInt32(X); 

  INT敏= Int32.Parse(x)的; 

或字符串的


$ B $的情况下, b

  INT敏; 
Int32.TryParse(S,出敏);



我很好奇,在其执行速度最快的已在转换方法的数据类型,而不仅仅是整数。我只是用INT作为一个例子



编辑:这情况下,从获取信息从数据表背起来。将(INT)仍然工作的最快?



从一些测试,如果对象x = 123123123,INT执行最快的,就像许多人所说。当x是一个字符串,解析运行速度最快的(注:铸造抛出异常)。我真的很好奇他们是如何运行时,通过以下方式检索的值:

 的foreach(DataRow的行someTable .Rows)
{
敏=(int)的行[一些int值];
myInt2 = Int.Parse(行[一些int值]);
myInt2 = Convert.ToInt32(行[一些int值]);
}


解决方案

为什么不干脆?尝试了几千次



(这也适用于所有的什么是最快的:问题)






嗯,很多downvotes多年来...我想我应该在这个答案扩展。



上面的语句与我的青春一定程度的浮躁的心态做的,但是我还是赞同其观点。这是不值得花时间创建一个SO质疑问别人什么,他们认为是快出把每小于1ms两个或三个操作。



这是事实的人会拿一两个比其他的周期较长,几乎肯定会在每天的日常使用可以忽略不计。而如果你发现在你的应用程序中的性能问题,当您转换数百万个对象为整数,这是的地步,你可以分析的的实际的代码,你会能够轻松地测试INT转换是否实际上是瓶颈。



和而今天它的对象-INT转换器,明天也许你会觉得你的对象,日期时间转换器走的是一条很长一段时间。你会创建另一个SO问题,找出什么是最快的方法是什么?



对于您的情况(毫无疑问,长久以来由现在已经解决),为在评论中提到的,你正在查询数据库,所以对象的INT转换是你最担心的。如果我是你,我会使用任何你所提到的转换方法。如果出现问题我会孤立通话,使用分析或记录。然后,当我注意到对象-INT转换正在做一百万次,由该转换所花费的总时间似乎相对较高,我会改变,以使用不同的转换方法,并重新分布。挑选花费最少的时间转换方法。你甚至可以在一个单独的解决方案进行测试,甚至LINQPad,或Powershell的等等。


Which of the following code is fastest/best practice for converting some object x?

int myInt = (int)x;

or

int myInt = Convert.ToInt32(x);

or

int myInt = Int32.Parse(x);

or in the case of a string 's'

int myInt;
Int32.TryParse(s, out myInt);

I'm curious on which performs the fastest for datatypes which have a method in Convert, not just ints. I just used int as an example.

Edit: This case arose from getting information back from a datatable. Will (int) still work the fastest?

From some testing, when object x =123123123, int performs the fastest, like many have said. When x is a string, Parse runs the fastest (note: cast throws an exception). What I am really curious is how they run when the value is being retrieved in the following way:

foreach(DataRow row in someTable.Rows)
{
    myInt = (int)row["some int value"];
    myInt2 = Int.Parse(row["some int value"]);
    myInt2 = Convert.ToInt32(row["some int value"]);
}

解决方案

Why don't you just try it a couple of thousand times?

(this goes for all "What is fastest:" questions)


Hmmm, lots of downvotes over the years... I guess I should expand on this answer.

The above statement was made with a degree of flippancy in my youth, however I still agree with its sentiment. It is not worthwhile spending time creating a SO question asking others what they think is faster out of two or three operations that take less than 1ms each.

The fact that one might take a cycle or two longer than the other will almost certainly be negligible in day-to-day usage. And if you ever notice a performance problem in your application when you are converting millions of objects to ints, that's the point where you can profile the actual code, and you'll easily be able to test whether the int conversion is actually the bottleneck.

And whereas today it's the object-int converter, tomorrow maybe you'll think your object-DateTime converter is taking a long time. Would you create another SO question to find out what's the fastest method?

As for your situation (no doubt long since resolved by now), as mentioned in a comment, you are querying a database, so object-int conversion is the least of your worries. If I was you I would use any of the conversion methods you mentioned. If a problem arises I would isolate the call, using a profiler or logging. Then when I notice that object-int conversion is being done a million times and the total time taken by that conversion seems relatively high, I would change to using a different conversion method and re-profile. Pick the conversion method that takes the least time. You could even test this in a separate solution, or even LINQPad, or Powershell etc.

这篇关于什么最快是:(INT),Convert.ToInt32(x)或Int32.Parse(x)的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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