new BigDecimal("10000");
new BigDecimal(10000);
I know the string constructor is used if the number is bigger than the compiler would accept, but are either of the constructors faster than the other?
You can look at source code.
public BigDecimal(int val) {
intCompact = val;
}
public BigDecimal(String val) {
this(val.toCharArray(), 0, val.length());
}
public BigDecimal(char[] in, int offset, int len) {
...very long
}
Obviously, who is faster.
Passing a String to the constructor of BigDecimal requires a parsing of the String and a check char by char.
Passing an int is faster because it results only in a single assignment.
In any case the time difference is not signifiant.
Here the code of BigDecimal with an int parameter:
public BigDecimal(int val) {
intCompact = val;
}
The code of BigDecimal constructor with a String calls BigDecimal(char[], int, int) that has around 140 rows of code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With