While recently reviewing a piece of code, I managed to spot three different methods being used for converting values stored in objects or strings into Integers. This is where Visual Basic takes the record for having the most methods for performing the same operation. Thinking about it I realised that I could name six different ways of converting strings to integers in Visual Basic…
This post is a "back to basics" investigation into which way is best to convert to an Integer.
Meet the methods:
CInt, CType and Int belong to the Microsoft.VisualBasic namespace. Convert.Int32, Integer.Parse and Integer.TryParse are from the main body of the .Net Framework.
To find the best way of converting a string to an integer I have run each of the methods through a series of tests; the speed of each method, its ability to round number, handle Nothing (null) and completely invalid values.
Test 1 – Speed
In order to find the fastest method of converting a string to an integer each method was put through its paces converting the string “100” into an integer. To be able to see the difference in timings the operation was performed 100 million times. Each test run was carried out multiple times to achieve an average timing. Each batch of test runs were performed against three different version of the .Net Frameworks, 2.0, 3.5 and 4.0.
(These timings are specific to my PC and should only be used for comparison with each other)
The fastest method for .Net 2.0 and .Net 3.5 is Integer.Parse at 13.12 seconds and 13.23 seconds respectively, but is overtaken for .Net 4.0 by Integer.TryParse at 13.19 seconds.
It is nice to know that using the TryParse method does not mean you are getting the performance hit you might imagine for the extra checking. (During testing a Boolean variable was being set to the result of the parse test).
CInt, CType and Int performed badly in comparison to the other methods. This is conceivably due to these methods being there for backwards compatibility with old VB6 code, although as you will see in the next few tests they do more than the other methods.
Test 2 - Rounding
Sometime it is desirable to round values that are not whole numbers into integers, for this test each method was passed “0.5” and “0.6” to determine if it could successfully convert the value to an integer, and whether it would round the number up or down.
| Method | 0.5 | 0.6 |
| CInt | 0 | 1 |
| Convert.ToInt32 | Exception | Exception |
| CType | 0 | 1 |
| Int | 0 | 0 |
| Integer.Parse | Exception | Exception |
| Integer.TryParse | Failed | Failed |
Only the Microsoft.VisualBasic methods handled the test, the others threw exceptions apart from Integer.TryParse which, being designed for this scenario, reported a failure.
Also it is interesting to note that Int will always returns the integer portion of the number, and so always rounds down for positive numbers and up for negative numbers. In researching this post I came across, if you can believe it, a seventh method of converting a string to an integer called Fix, which is very similar to Int but handles negative numbers differently.
Test 3 – Nothing (null)
These are the results from attempting to convert a string set to Nothing into an integer.
| Method | Result |
| CInt | 0 |
| Convert.ToInt32 | 0 |
| CType | 0 |
| Int | ArgumentNullException |
| Integer.Parse | ArgumentNullException |
| Integer.TryParse | Failed (0) |
Interestingly the first three methods consider Nothing to be the same as zero, while once again Integer.TryParse rejects the value.
Test 4 – Invalid Integer
Strings can of course contain text as well as numbers, the following results are when attempting to convert the string “abc” into an integer.
| Method | Result |
| CInt | InvalidCastException |
| Convert.ToInt32 | FormatException |
| CType | InvalidCastException |
| Int | InvalidCastException |
| Integer.Parse | FormatException |
| Integer.TryParse | Failed |
Well I think from these results that it is fairly obvious that the language designers do not think it is a good idea to be able to pass any old string into an integer conversion. I agree, I would prefer my applications to fail fast rather than perhaps silently returns zeros when passed random text (as with Nothing above).
Overall Results
So which is best and why? Well it all comes down to how you are going to be using the function and what sort of data you want the function to handle.
Unless you want the specific rounding abilities of Int then I can’t see any reason to ever use it. It performed badly against every test, making it the worst overall.
If you need speed then you should ignore the Microsoft.VisualBasic methods, as they are at least twice as slow as the others.
If you need to handle general rounding then CInt and CType are the only functions for you.
If the data being presented for conversion may not be an integer, then use Integer.TryParse to successfully determine this without throwing an exception, in the fastest time. The only catch with this method is that you need to remember to handle the function passing back a failure result.
Finally if you want the speed without the extra coding complexity that goes with handling the result from Integer.TryParse, the Integer.Parse looks best.