Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Faster BigDecimal

416044Dec 27 2006 — edited Jul 20 2010
I'm looking for a faster implementation of BigDecimal than java.math.BigDecimal as I need the higher precision (at least MathContext.DECIMAL128) and of course stay pure Java (so no JNI).

I found a (rather old) IBM site with a IBM Version of BigDecimal being several times faster. But at least with the latest JDK 1.6, this doesn't seem to be true anymore. I also tested the eval version of ArciMath but the latest news on the homepage is alread 4.5 years old and I don't know if it's compatible with the latest Java 6.
         System.out.println("Test: BigDecimal performance");
        final long iterations = 10000000;
        
        long t = System.currentTimeMillis();
        double d = 123.456;
        for (int i = 0; i < iterations; i++) {
            final double b = d * System.currentTimeMillis() * System.currentTimeMillis();
        }
        System.out.println("double: "+(System.currentTimeMillis() - t));

        
        t = System.currentTimeMillis();
        BigDecimal bd = new BigDecimal("123.456");
        for (int i = 0; i < iterations; i++) {
            final BigDecimal b = bd.multiply(new BigDecimal(""+System.currentTimeMillis())).add(new BigDecimal(""+System.currentTimeMillis()));
        }
        System.out.println("java.math.BigDecimal: "+(System.currentTimeMillis() - t));
        
        t = System.currentTimeMillis();
        com.ibm.math.BigDecimal bd2 = new com.ibm.math.BigDecimal("123.456");
        for (int i = 0; i < iterations; i++) {
            final com.ibm.math.BigDecimal b = bd2.multiply(new com.ibm.math.BigDecimal(""+System.currentTimeMillis())).add(new com.ibm.math.BigDecimal(""+System.currentTimeMillis()));
        }
        System.out.println("com.ibm.math.BigDecimal: "+(System.currentTimeMillis() - t));
        
        t = System.currentTimeMillis();
        be.arci.math.BigDecimal bd3 = new be.arci.math.BigDecimal("123.456");
        for (int i = 0; i < iterations; i++) {
            final be.arci.math.BigDecimal b = bd3.multiply(new be.arci.math.BigDecimal(""+System.currentTimeMillis())).add(new be.arci.math.BigDecimal(""+System.currentTimeMillis()));
        }
        System.out.println("be.arci.math.BigDecimal: "+(System.currentTimeMillis() - t));
Output:
Test: BigDecimal performance
double: 1485
java.math.BigDecimal: 40967
com.ibm.math.BigDecimal: 43748
be.arci.math.BigDecimal: 33373
I read that BigDecimal got improved in JDK 1.5/1.6. But it's still so very much slower when I change from doubles to BigDecimal (with MathContect.DECIMAL128). Are there any more BigDecimal implementations that are faster than java.math.BigDecimal?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 17 2010
Added on Dec 27 2006
27 comments
4,176 views