Is there a faster way to convert a string
to double
than Convert.ToDouble
?
I have monitored System.Conv
If you are 100% sure about your source data format and range, you can use:
string num = "1.34515";
int len = num.Length - num.IndexOf('.') - 1;
int intval = Int32.Parse(num.Replace(".", ""));
double d = (double)intval / PowersOf10[len]; // PowersOf10 is pre-computed or inlined
It worked ~50% faster than Double.Parse
for me, but I wouldn't use it in any serious applications - it's extremely limited compared to proper parsing and I can't think of process where you need to parse millions of doubles and a few milliseconds would make the difference.