I’m trying to make a method faster. Right now, it takes an object obj and converts it to a double with the following:
double val = Convert.ToDouble(obj);
Would it be faster if I did this?
double val = double.Parse(obj.ToString());
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
For
stringConvert.ToDoublejust checks value fornulland return0.0if so. Then it really callsdouble.Parse:So I don’t think that
double.Parseif much faster for non-number types. But when you don’t really know that type your argument is, it’s better to callConvert.ToDouble(value)instead ofdouble.Parse(value.ToString()).