- Home /
Most efficient way to convert string[] to int [] in C#?
Hi there. I have here a short string[], with each of those strings just being a single whole number. I have seen lots of different ways people have been converting those into int[], but does anyone know which method is the most efficient?
Create different script for different methods. Attach them to an empty object. duplicate that item thousand times. run your game. Check in profiler which script takes least amount of time. theres your most efficient way.
Thats not the way to go. Use deep profiler to test the costs of a specific method.
Answer by NeverHopeless · Aug 19, 2015 at 12:30 PM
I didn't do a performance test before but this one i uses, since it is a bit shorter way, but this link can be use for performance benchmark, i added two alternatives, you can add the remaining and pick the best one:
string[] test = new string[] {"1", "2", "3", "4", "5"};
int[] arr = Array.ConvertAll<string, int>(test, int.Parse);
thanks! converting arrays like this happens extremely frequently in my script, so its a big help.
Answer by Driseus · Aug 19, 2015 at 12:20 PM
Test the performance of your program and do major optimization before you think about such micro optimizations.
Your answer
Follow this Question
Related Questions
Converting a string to an int 2 Answers
Get JSON array object string value 2 Answers
String Parsing 1 Answer
Convert Array to String 3 Answers
Assigning String Value From Array in separate script sets value to null. 2 Answers