What to do with Java BigDecimal performance?

41,150

Solution 1

May be you should start with replacing a = (1/b) * c with a = c/b ? It's not 10x, but still something.

If I were you, I'd create my own class Money, which would keep long dollars and long cents, and do math in it.

Solution 2

Most double operations give you more than enough precision. You can represent $10 trillion with cent accuracy with double which may be more than enough for you.

In all the trading systems I have worked on (four different banks), they have used double with appropriate rounding. I don't see any reason to be using BigDecimal.

Solution 3

So my original answer was just flat out wrong, because my benchmark was written badly. I guess I'm the one who should have been criticized, not OP ;) This may have been one of the first benchmarks I ever wrote... oh well, that's how you learn. Rather than deleting the answer, here are the results where I'm not measuring the wrong thing. Some notes:

  • Precalculate the arrays so I don't mess with the results by generating them
  • Don't ever call BigDecimal.doubleValue(), as it's extremely slow
  • Don't mess with the results by adding BigDecimals. Just return one value, and use an if statement to prevent compiler optimization. Make sure to have it work most of the time to allow branch prediction to eliminate that part of the code, though.

Tests:

  • BigDecimal: do the math exactly as you suggested it
  • BigDecNoRecip: (1/b) * c = c/b, just do c/b
  • Double: do the math with doubles

Here is the output:

 0% Scenario{vm=java, trial=0, benchmark=Double} 0.34 ns; ?=0.00 ns @ 3 trials
33% Scenario{vm=java, trial=0, benchmark=BigDecimal} 356.03 ns; ?=11.51 ns @ 10 trials
67% Scenario{vm=java, trial=0, benchmark=BigDecNoRecip} 301.91 ns; ?=14.86 ns @ 10 trials

    benchmark      ns linear runtime
       Double   0.335 =
   BigDecimal 356.031 ==============================
BigDecNoRecip 301.909 =========================

vm: java
trial: 0

Here's the code:

import java.math.BigDecimal;
import java.math.MathContext;
import java.util.Random;

import com.google.caliper.Runner;
import com.google.caliper.SimpleBenchmark;

public class BigDecimalTest {
  public static class Benchmark1 extends SimpleBenchmark {
    private static int ARRAY_SIZE = 131072;

    private Random r;

    private BigDecimal[][] bigValues = new BigDecimal[3][];
    private double[][] doubleValues = new double[3][];

    @Override
    protected void setUp() throws Exception {
      super.setUp();
      r = new Random();

      for(int i = 0; i < 3; i++) {
        bigValues[i] = new BigDecimal[ARRAY_SIZE];
        doubleValues[i] = new double[ARRAY_SIZE];

        for(int j = 0; j < ARRAY_SIZE; j++) {
          doubleValues[i][j] = r.nextDouble() * 1000000;
          bigValues[i][j] = BigDecimal.valueOf(doubleValues[i][j]); 
        }
      }
    }

    public double timeDouble(int reps) {
      double returnValue = 0;
      for (int i = 0; i < reps; i++) {
        double a = doubleValues[0][reps & 131071];
        double b = doubleValues[1][reps & 131071];
        double c = doubleValues[2][reps & 131071];
        double division = a * (1/b) * c; 
        if((i & 255) == 0) returnValue = division;
      }
      return returnValue;
    }

    public BigDecimal timeBigDecimal(int reps) {
      BigDecimal returnValue = BigDecimal.ZERO;
      for (int i = 0; i < reps; i++) {
        BigDecimal a = bigValues[0][reps & 131071];
        BigDecimal b = bigValues[1][reps & 131071];
        BigDecimal c = bigValues[2][reps & 131071];
        BigDecimal division = a.multiply(BigDecimal.ONE.divide(b, MathContext.DECIMAL64).multiply(c));
        if((i & 255) == 0) returnValue = division;
      }
      return returnValue;
    }

    public BigDecimal timeBigDecNoRecip(int reps) {
      BigDecimal returnValue = BigDecimal.ZERO;
      for (int i = 0; i < reps; i++) {
        BigDecimal a = bigValues[0][reps & 131071];
        BigDecimal b = bigValues[1][reps & 131071];
        BigDecimal c = bigValues[2][reps & 131071];
        BigDecimal division = a.multiply(c.divide(b, MathContext.DECIMAL64));
        if((i & 255) == 0) returnValue = division;
      }
      return returnValue;
    }
  }

  public static void main(String... args) {
    Runner.main(Benchmark1.class, new String[0]);
  }
}

Solution 4

Assuming you can work to some arbitrary but known precision (say a billionth of a cent) and have a known maximum value you need handle (a trillion trillion dollars?) you can write a class which stores that value as an integer number of billionths of a cent. You'll need two longs to represent it. That should be maybe ten times as slow as using double; about a hundred times as fast as BigDecimal.

Most of the operations are just performing the operation on each part and renormalizing. Division is slightly more complicated, but not much.

EDIT:In response to the comment. You will need to implement a bitshift operation on your class (easy as along as the multiplier for the high long is a power of two). To do division shift the divisor until it's not quite bigger than the dividend; subtract shifted divisor from dividend and increment the result (with appropriate shift). Repeat.

EDIT AGAIN:You may find BigInteger does what you need here.

Solution 5

Store longs as the number of cents. For example, BigDecimal money = new BigDecimal ("4.20") becomes long money = 420. You just have to remember to mod by 100 to get dollars and cents for output. If you need to track, say, tenths of a cent, it'd become long money = 4200 instead.

Share:
41,150
Alexander Temerev
Author by

Alexander Temerev

Updated on December 27, 2021

Comments

  • Alexander Temerev
    Alexander Temerev over 2 years

    I write currency trading applications for living, so I have to work with monetary values (it's a shame that Java still doesn't have decimal float type and has nothing to support arbitrary-precision monetary calculations). "Use BigDecimal!" — you might say. I do. But now I have some code where performance is an issue, and BigDecimal is more than 1000 times (!) slower than double primitives.

    The calculations are very simple: what the system does is calculating a = (1/b) * c many many times (where a, b and c are fixed-point values). The problem, however, lies with this (1/b). I can't use fixed point arithmetic because there is no fixed point. And BigDecimal result = a.multiply(BigDecimal.ONE.divide(b).multiply(c) is not only ugly, but sluggishly slow.

    What can I use to replace BigDecimal? I need at least 10x performance increase. I found otherwise excellent JScience library which has arbitrary-precision arithmetics, but it's even slower than BigDecimal.

    Any suggestions?

  • Alexander Temerev
    Alexander Temerev about 15 years
    And implement division, rounding, exponentiation etc. myself from scratch? :)
  • sfossen
    sfossen about 15 years
    that's adding even more operations. so that would be slower.
  • Ryan Graham
    Ryan Graham about 15 years
    Yes, I believe that's what he's suggesting.
  • Alexander Temerev
    Alexander Temerev about 15 years
    I need to track (in intermediate calculations) billionths of cents. Let's say we have a quote for USD/JPY: 99.223. Somewhere else I will need a JPY/USD quote, which is around 0.0100779022 (I need even more precision).
  • Joachim Sauer
    Joachim Sauer about 15 years
    @sfossen: more operations than what? BigDecimal? Definitely not. BigDecimal doesn't use long to store it's value (because the value can be almost arbitrary large). Using longs is definitely faster than BigDecimal.
  • Alexander Temerev
    Alexander Temerev about 15 years
    This is quite difficult task to get it right (if you doubt, take a look in Java Math classes). I don't believe no one else does high-performance monetary calculations in Java.
  • Vladimir Dyuzhev
    Vladimir Dyuzhev about 15 years
    It's a hard task to do it for a general-purpose library. For specific application (which uses only a subset) of operations it's trivial. In fact, I have such as class in my own app, and it only need 5 or 6 common operations.
  • Pesto
    Pesto about 15 years
    @Alexander: long is 64 bits, which mean a max value of 2^63 - 1, or roughly 9.22 x 10^18. If you want ten digits after the decimal in dollar terms, you get a max value of somewhere in the neighborhood of $9.22 x 10^8. You can decide if that's large enough for you.
  • Vladimir Dyuzhev
    Vladimir Dyuzhev about 15 years
    The gain would be essentially in that for computations you'd be using double*long, which is native op, and thus fast. E.g. USD/JPY * 1000 yen => double * long. If double covers your precision when multiples to biggest money amounts you have -- you're OK.
  • Alexander Temerev
    Alexander Temerev about 15 years
    @Pesto: $9.22 x 10^8 is 90 billion. It's a normal daily trading volume on mid-range forex marketplaces.
  • sfossen
    sfossen about 15 years
    @Pesto: missed the long conversion, however, 2 decimal points is almost never acceptable in monetary calculations, although similar to my suggestion of fixed point math.
  • DJClayworth
    DJClayworth about 15 years
    If you write currency trading apps for a living, these calculations are your 'core functionality'. You will need to spend time and effort getting them right to give yourself a competitive advantage.
  • Alexander Temerev
    Alexander Temerev about 15 years
    Will you suggest me an algorithm for division in this case?
  • Alexander Temerev
    Alexander Temerev about 15 years
    DJClayworth: This is what I trying to do, after BigDecimal proved to be not enough for my needs.
  • sfossen
    sfossen about 15 years
    Using a fixed point library will get you speed, but you will lose some precision. You could try using BigInteger to make a fixed point library.
  • Pesto
    Pesto about 15 years
    @sfossen: Which is why I mentioned that in my original answer, and why Alexander and I just had this whole conversation about using additional decimal places.
  • sfossen
    sfossen about 15 years
    @Pesto: Ya, a single primitive won't be enough, which is why I suggested a fixed point library.
  • sfossen
    sfossen about 15 years
    Also don't use a power of ten, if you do this, use a power of 2. power of ten easier for humans but harder for computers :P
  • Kevin Day
    Kevin Day about 15 years
    it is unlikely that JNI will help performance here, unless the calculations can be batched. JNI introduces significant overhead as you cross the JVM/native boundary.
  • Anne Porosoff
    Anne Porosoff about 15 years
    You are correct that the boundary does have a slowdown and I've definitely felt that pain but if BigDecimal truly has the claimed 1000x slowdown and JNI was only a fraction, it may be worth it.
  • Vladimir Dyuzhev
    Vladimir Dyuzhev about 15 years
    From browsing BigDecimal class it seems it wastes a considerable amount of time building a new object for every operation (e.g. because it's immutable). I guess if a mutable implementation existed, it would be faster. Not 10 times though, alas.
  • Hannele
    Hannele over 12 years
    Rounding results decreases accuracy, rather than increasing it.
  • maaartinus
    maaartinus almost 12 years
    Yes, the precision of double is more than sufficient. I do such things too, end it works perfectly unless I forget to round and the customer sees something like -1e-13 where they expect a non-negative result.
  • maaartinus
    maaartinus almost 12 years
    @Hannele Most of the time yes, but sometimes it indeed increases it. For example, when computing sum of prices where each of them is given with two decimal places, the rounding to two decimal places guarantees a correct result (unless you're summing many billions of values).
  • Vishy
    Vishy almost 12 years
    I have since designed three different trading systems for different funds and used double for prices or long cents.
  • Hannele
    Hannele almost 12 years
    @maaartinus You have an interesting point! However, I don't believe that's directly applicable to the OP (division).
  • maaartinus
    maaartinus almost 12 years
    @Hannele: Agreed, rounding helps only if you know how many decimal places the result should have which is not the case with division.
  • Jim
    Jim almost 11 years
    Doubles are fractional binary approximations of numbers and, as such, almost no fractional decimal numbers have an exact fractional binary representation. Unless the fractional part is some multiple of a negative power of 2, the representation in a double is an approximation. Decimal fractions that are multiples of negative powers of two are extremely rare compared to the total number of decimal fractions down to billionths. Sums of several thousand of these approximations will very quickly have issues that will start to show up in cents and make rounding/truncation solutions difficult.
  • supercat
    supercat over 10 years
    If double values are scaled in a manner such that any domain-required rounding is always to a whole number, then any rounded values will be "exact" unless they're really big. For example, if things that will round to the nearest $0.01 are stored as a number of pennies rather than dollars, double will can penny-rounded amounts precisely unless they exceeed $45,035,996,273,704.96.
  • supercat
    supercat over 10 years
    Use of a base-ten type rather than a base-2 type is only helpful for representing things whose true value is an exact fraction with a base-ten denominator; even there, suitably-scaled base-2 types can still offer equal accuracy with better performance, though one must use care to avoid scaling mistakes.
  • maaartinus
    maaartinus about 10 years
    +1 for making the benchmark but -1 for the implementation. You're mostly measuring how long it takes to create a BigDecimal... or more exactly, the creation overhead is present in all benchmarks and may dominate them. Unless it's what you wanted (but why?), you'd need to pre-create the values and store in an array.
  • maaartinus
    maaartinus about 10 years
    @Jim: I disagree. A double has nearly 16 digits. Assume 9 of them before the decimal point, 2 after, and you're left with 5 to burn... in a worst case you could do 105 operation before it blows, assuming random round-off error distribution average case it'd be 1010.
  • durron597
    durron597 about 10 years
    @maaartinus Well this is embarrassing, I have gotten so much better at writing benchmarks in the last 14 months. I'll edit the post now
  • maaartinus
    maaartinus about 10 years
    +1 now the values make sense! I'm not sure about what you're doing with the if. It probably won't get optimized away but it may. I used to do something like result += System.identityHashCode(o) but then I discovered the JMH BlackHole.
  • Jim
    Jim about 10 years
    @maaartinus - You express the thinking that has buried many in a sea of woes when they used doubles when they need EXACT decimal fractions. Your 16 digits is ALWAYS a binary approximation of a decimal fraction and it is the approximation that will eventually be your downfall. Systems that deal in financials must ALWAYS have exact decimal fractions where the distributive properites of addition, subtraction, multiplication, and division hold to EXACT decimal fractions. x(a+b+c) eventually does NOT have the same result as xa+xb+xc with doubles used for decimal fractions. Here endeth the lesson.
  • maaartinus
    maaartinus about 10 years
    @Jim: Starting with exact decimal numbers, it's always possible to obtain the exact result via rounding the approximation as long as the approximation is good enough. The lack of distributive and associative properties doesn't matter as long as I can round the approximation to the exact result. I agree that there are computation for which double is too imprecise, but they're rather rare.
  • Jim
    Jim about 10 years
    @maaartinus: I have extensive experience in a cellular billing system which originally used doubles that rounding and truncation, no matter how attempted, cannot always resolve a computation to an exact decimal result. Errors occurred frequently even if they were statistically rare. Statistical rarities amongst thousands of computations for each of millions of customers a month. is still a really big number. Cases that reproduced them were trivial to create. Rarity is not acceptable. The only acceptable computation errors for accounting acceptance and certification are IMPOSSIBLE and NEVER.
  • maaartinus
    maaartinus about 10 years
    @Jim: By rare I didn't mean that a double-using computation may fail from time to time, but rather that it often works perfectly (and provably by error bounds analysis). With some tasks it does not work, just as yours.
  • Amrinder Arora
    Amrinder Arora about 7 years
    @maaartinus Could you tell more about the JMH blackhole please?
  • maaartinus
    maaartinus about 7 years
    @AmrinderArora Not really. The Blackhole is a pretty complicated thing doing something with the input, so it can't be optimized away. It's optimized for speed even in the multithreaded case.
  • maaartinus
    maaartinus almost 7 years
    @Jim I'm yet to see a realistic example when it can not work and there's a small bounty for it. There are people using doubles with success.