[英]Convert.ToString(…) or Object.ToString() for performance
Suppose I have a loop which is going to convert an ArrayList
with 10 million elements, filled with int
, to an array of string
. 假设我有一个循环是要转换一个ArrayList
有10万台,充满int
,以数组string
。 Should I be using Convert.ToString(...)
or Object.ToString()
? 我应该使用Convert.ToString(...)
还是Object.ToString()
? Is it true that in this case Convert.ToString(...)
unboxes the elements and decreases the performance? 在这种情况下, Convert.ToString(...)
是否将元素解包并降低性能?
If you've got an ArrayList
, any value types will already be boxed. 如果您有一个ArrayList
,则任何值类型都将被装箱。 Why are you using ArrayList
rather than a List<int>
? 为什么使用ArrayList
而不是List<int>
? The latter will avoid both the execution time cost of boxing and the significant space implications. 后者将避免拳击的执行时间成本和重要的空间影响。
After changing to use List<int>
however, I'd then just call ToString
. 在更改为使用List<int>
之后,我只需调用ToString
。 It says exactly what you want to do in a simpler way than Convert.ToString
, IMO... and provides more formatting options. 它以比Convert.ToString
,IMO更简单的方式准确地说出你想做的事情,并提供更多的格式化选项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.