Consider an application that
- Reads several thousands of
String
values from a text file. - Selects (via a regular expression match) those values that represent a number (from simple integers to very large values written in scientific notation with mantissa).
- For each
String
value representing a number, instantiates aBigDecimal
object (at a total rate of thousands of Bigdecimal objects per second). - Uses each instantiated
BigDecimal
object for further processing.
Given the above scenario, obviously the instantiation of each BigDecimal
object has an impact on performance.
One way to instantiate those BigDecimal
objects from a non-null String str
, is:
BigDecimal number = new BigDecimal(str.toCharArray(), 0, str.length()));
which is exactly what the String constructor of BigDecimal expands to in the Oracle
implementation of the JDK
.
Is there a faster way of instantiating BigDecimal
objects from such strings, or via an alternative approach?