Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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?

like image 381
DeveloperInToronto Avatar asked Dec 27 '22 14:12

DeveloperInToronto


1 Answers

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.

like image 52
Jon Skeet Avatar answered Dec 30 '22 11:12

Jon Skeet