C# 类型转换性能

C# 类型转换性能,c#,types,type-conversion,C#,Types,Type Conversion,这两种转换的执行时间是相同还是不同?转换是如何由.net完成的 float j = r; decimal e = Convert.ToDecimal(j);in convert class using todecimal method is converting to decimal decimal tt=(decimal)j;// How does this work in casting (Exactly How boxing and unboxing works) 没有区别,因为使用的

这两种转换的执行时间是相同还是不同?转换是如何由.net完成的

float j = r;
decimal e = Convert.ToDecimal(j);in convert class using todecimal method is converting to decimal
decimal tt=(decimal)j;// How does this work in casting (Exactly How boxing and unboxing works) 

没有区别,因为使用的代码与您看到的完全相同

但一般来说,你应该先自己测量。下面是你会说的:


如果你有两匹马,你想知道这两匹马中哪一匹跑得更快,那就去比赛吧

如果您查看它们的调用堆栈,您将看到它们

decimal tt=(decimal)j;
比的函数调用少1个

decimal e = Convert.ToDecimal(j);
因为我之前的那个人在回答中提到的原因,我会在评论中提到这一点,但我没有50个代表,所以我不能

所以非常轻微

decimal tt=(decimal)j;

速度更快。

你为什么不自己测量一下呢?默认答案:如果你有两匹马,你想知道哪匹马跑得更快,那就去比赛。我看到了唯一的不同syntax@SonerGönülIf you decomile Convert.ToDecimalfloat value源是返回小数点;我很抱歉,但对我来说,这些问题很愚蠢-1!这是最好的微观优化,您很少会注意到显著的差异。这里有一张小支票可以证明这一点:
decimal tt=(decimal)j;