I want to use BigDecimal to represent arbitrary precision numbers like prices and amounts in a low-latency trading application with thousands of orders and execution reports per second.
I won't be doing many math operations on them, so the question is not about performance of the BigDecimal per se, but rather about how large volumes of BigDecimal objects would affect performance of the application.
My concern is that huge amount of short-lived BigDecimal objects will put a strain on a GC and result in larger Stop-The-World pauses in CMS collector - and this is definitely what I would like to avoid.
Can you please confirm my concerns and suggest alternatives to using BigD? Also, if you think my concerns are wrong - please explain why.
Update:
Thanks for all who answered. I am now convinced that using BigDecimal will hurt latency of my application (even though I still plan to measure it).
For the time being we decided to stick with "very non-OOP" solution (but without accuracy hit) - use two int
s, one for mantissa and another one for exponent. Rationale behind this is that primitives are placed on stack, not heap, and hence are not subject to garbage collection.
If you are developing a low-latency trading program and you genuinely want to compete in latency terms, then BigDecimal
is not for you, it is as simple as that. Where microseconds matter, object creation and any decimal math is just too expensive.
I would argue that for almost everyone else, using BigDecimal
is a no-brainer because it will have little visible impact on application performance.
In latency-critical systems making trading decisions, any unpredictable garbage-collection pauses are completely out-of-the-question so whilst the current garbage-collection algos are fantastic in normal use, they are not necessarily appropriate when a delay of 5 milliseconds may cost you a lot of money. I would expect that large systems were written in a very non-OOP style, with little or no objects being used aside from some interned Strings (for codes and the like).
You'll certainly need to use double
(or even float
) and take the accuracy hit, or else use long
and measure all amounts in cents, tenths of a cent or satoshis (whatever the smallest unit of account is).