Which is better to use: Convert.ToX or X.Parse (...)?

I use reflection to create some objects. The values ​​that I set are read from the file, so they are initially in string format, and I need to convert them to the property data type.

My faster / better question is: Convert.ToX(...) methods or X.Parse(...) methods?

+6
source share
5 answers

All Convert.ToX functions that take an argument of type string ultimately access the Parse method of the corresponding data type.

For example, Convert.ToInt32(string) looks something like this:

 public static int ToInt32(string value) { if (value == null) { return 0; } return int.Parse(value, CultureInfo.CurrentCulture); } 

The same goes for all other digital conversion methods, including Decimal and DateTime . So it doesn’t matter which one you use; the result (and speed) will be the same anyway.

Indeed, the only difference is the if (value == null) guard clause at the beginning. Regardless of whether it is convenient, it depends on the specific use case. Generally, if you know that you have a non-zero string object, you can also use Parse . If you're not sure, ConvertToX is a safer bet requiring fewer zero checks.

+11
source

They are exactly the same! Convert.ToX(String) methods actually call X.Parse(String) methods.

+1
source

According to what I see in Reflector, the Convert form string is a wrapper around Parse. so prudent use of parsing is marginally better in performance.

EDIT: after Cody said that optimization would not change much, I tested it on my machine, and indeed, the runtime for Parse and Convert got the same when analyzing 1 million inetgers in a loop.

EDIT2: here you go yas4891, this is actually the code that you used with very minor changes.

 public static void Main() { int tRuns = 1000000; List<String> tList = new List<string>(); for (int i = 0; i < tRuns; i++) tList.Add(i.ToString()); Stopwatch s = new Stopwatch(); s.Start(); int tSum = 0; for (int i = tRuns - 1; i >= 0; i--) { tSum += Convert.ToInt32(tList[i]); } s.Stop(); Console.WriteLine("convert: " + s.ElapsedMilliseconds); Console.WriteLine("tSum:" + tSum); s.Reset(); s.Start(); tSum = 0; for (int i = tRuns - 1; i >= 0; i--) { tSum += Int32.Parse(tList[i]); } s.Stop(); Console.WriteLine("parse: " + s.ElapsedMilliseconds); Console.WriteLine("tSum:" + tSum); Console.ReadKey(); } 
+1
source

Another possibility is TryParse methods. They are especially useful if it is likely that the value cannot be successfully analyzed. Instead of throwing an exception, the call returns a bool indicating whether the operation was successful. This is much faster and a cleaner implementation than eliminating the exception.

+1
source

using the following code

 int tRuns = 1000000; List<String> tList = new List<string>(); for (int i = 0; i < tRuns; i++) tList.Add(i.ToString()); PerformanceMeter.Start(); int tSum = 0; for (int i = tRuns-1; i >= 0; i--) { tSum += Convert.ToInt32(tList[i]); } PerformanceMeter.LogAndStop("using Convert.ToInt32:"); cLogger.Info("tSum:" + tSum); PerformanceMeter.Start(); tSum = 0; for (int i = tRuns-1; i >= 0; i--) { tSum += Int32.Parse(tList[i]); } PerformanceMeter.LogAndStop("using Int32.Parse:"); cLogger.Info("tSum:" + tSum); 

gives me the following result:

{PerformanceMeter}: 178 INFO: - using Convert.ToInt32 :: 233.0133 ms
{Program}: 92 INFO: - tSum: 1783293664
{PerformanceMeter}: 178 INFO: - using Int32.Parse :: 179.0103 ms
{Program}: 102 INFO: - tSum: 1783293664

So, at least for Int32, using Int32.Parse seems more efficient. However, this may be different in your scenario, and I suppose you should perform a similar test.

-1
source

Source: https://habr.com/ru/post/887932/


All Articles