2

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;
4

2 に答える 2

8

は、Java SE の類似物ではありません。

努力する価値があるかどうかという質問については、このコードがパフォーマンスのボトルネックであることが証明されている場合にのみ、これを調べる必要があります。

于 2009-10-07T12:45:30.047 に答える
7

ここでドナルド・クヌースを引用します:

「私たちは小さな効率を忘れるべきです。たとえば、約 97% の確率で: 時期尚早の最適化はすべての悪の根源です。」

本当に測定可能な (!) 問題になるまでは、心配しないでください。私は BigDecimal のパフォーマンスの専門家ではありませんが、文字列の連結中に行われる char[] のコピーは、はるかに大きなオーバーヘッドになることは確かです。

于 2009-10-07T12:45:19.803 に答える