Analog of StringBuilder for BigDecimal

我的梦境 提交于 2019-12-11 04:25:05

问题


I've got a list of BigDecimals to sum. If they were Strings to concatenate, I would use StringBuilder to reduce object creation. Is there something similar for BigDecimal? Or maybe I shouldn't bother about it? Is optimization of BigDecimal creation worth putting effort to it?

BigDecimal result = BigDecimal.ZERO;
for (CashReportElement element : getReportElementSet()) {
    if (element.getCurrencyCode().equals(currencyCode)) {
        result = result.add(element.getSum());
    }
}
return result;

回答1:


The is no such analog in Java SE.

And on the question if its worth putting effort in it: You should look into this only if this code has been proven to be a performance bottleneck.




回答2:


I'd cite Donald Knuth here:

"We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil."

Don't worry until it really is a measurable (!) problem. I'm not an expert for BigDecimal performance, but copying of char[] which is done during String concatenation is a much bigger overhead, that's for sure.



来源:https://stackoverflow.com/questions/1531377/analog-of-stringbuilder-for-bigdecimal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!