Convert.ToString(...) or Object.ToString() for performance
Suppose I have a loop which is going to convert an ArrayList
with 10 million elements, filled wi开发者_StackOverflow社区th int
, to an array of string
. Should I be using Convert.ToString(...)
or Object.ToString()
? Is it true that in this case Convert.ToString(...)
unboxes the elements and decreases the performance?
If you've got an ArrayList
, any value types will already be boxed. Why are you using ArrayList
rather than a 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
. It says exactly what you want to do in a simpler way than Convert.ToString
, IMO... and provides more formatting options.
精彩评论