MCMastery
MCMastery

Reputation: 3249

BigDecimal constructor performance - string vs numeric

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?

Upvotes: 2

Views: 3090

Answers (5)

Chetan Kinger
Chetan Kinger

Reputation: 15212

I know the string constructor is used if the number is bigger than the compiler would accept

That's not the only reason for using the String constructor. Another reason for using the String constructor is to keep the value as-is when the BigDecimal is created.

If you don't really care about precision, you should stick to using double instead of BigDecimal.

but are either of the constructors faster than the other

This is something you have to find out yourself by benchmarking your code. That being said, you should prefer using the valueOf method instead of creating a new BigDecimal since valueOf will return cached values. (Currently, this ranges from 0 to 10 but this range can be higher for different JVM implementations and future implementations of the HotSpot VM as well so you are better of using valueOf)

Upvotes: 4

Zephyr Guo
Zephyr Guo

Reputation: 1173

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.

Upvotes: 4

AnkeyNigam
AnkeyNigam

Reputation: 2820

Find the time difference yourself & analyse. Like :-

BigDecimal big = null;
long startTime = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
    big = new BigDecimal("10000");
}
long endTime = System.currentTimeMillis();
System.out.println("time Taken In String : " + (endTime - startTime));

startTime = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
    big = new BigDecimal(10000);
}
endTime = System.currentTimeMillis();
System.out.println("time Taken in Numeric : " + (endTime - startTime));

PS:- Keep in consideration the Garbage Collection. Consider the above code as a reference only.

Upvotes: 0

Matt
Matt

Reputation: 879

The Constructor new BigDecimal(10000) should be faster because it does not have to convert a string into a number. The under-the-hood there has to be a cast or another call to a string function which would cause some additional overhead.

public class benchmark {

    public static void main(String [ ] args) {
        timeFunction();
    }

static void timeFunction() {
     long startTime = System.nanoTime();

     BigDecimal v1 = new BigDecimal("10000");
     System.out.println("new BigDecimal(\"10000\") : " + (System.nanoTime() - startTime) / 1000000 + " ms");

     startTime = System.nanoTime();

     BigDecimal v2 = new BigDecimal(10000);
     System.out.println("new BigDecimal(10000) : " + (System.nanoTime() - startTime) / 1000000 + " ms");
}

}

OUTPUT

new BigDecimal("10000") : 4 ms
new BigDecimal(10000) : 0 ms

Upvotes: 1

Davide Lorenzo MARINO
Davide Lorenzo MARINO

Reputation: 26946

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.

Upvotes: 4

Related Questions