summaryrefslogtreecommitdiff
path: root/amount.cc
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2005-03-08 04:52:12 +0000
committerJohn Wiegley <johnw@newartisans.com>2008-04-13 02:41:06 -0400
commit675319a0b01416a946407213e5c270a644adc575 (patch)
tree4c70882ac4076dfc217a67eca595d66a67285844 /amount.cc
parent6a1071b2890a338a8c378253ac54f52e57006aee (diff)
downloadfork-ledger-675319a0b01416a946407213e5c270a644adc575.tar.gz
fork-ledger-675319a0b01416a946407213e5c270a644adc575.tar.bz2
fork-ledger-675319a0b01416a946407213e5c270a644adc575.zip
(value): Use `std::map<>::lower_bound' to search for the nearest price
item in a commodity's history list. This is much more efficient than doing a reverse linear search.
Diffstat (limited to 'amount.cc')
-rw-r--r--amount.cc17
1 files changed, 11 insertions, 6 deletions
diff --git a/amount.cc b/amount.cc
index 35f50b21..05421760 100644
--- a/amount.cc
+++ b/amount.cc
@@ -1173,15 +1173,20 @@ amount_t commodity_t::value(const std::time_t moment)
std::time_t age = 0;
amount_t price;
- if (history)
- for (history_map::reverse_iterator i = history->prices.rbegin();
- i != history->prices.rend();
- i++)
- if (moment == 0 || std::difftime(moment, (*i).first) >= 0) {
+ if (history) {
+ if (moment == 0) {
+ history_map::reverse_iterator i = history->prices.rbegin();
+ age = (*i).first;
+ price = (*i).second;
+ } else {
+ history_map::iterator i = history->prices.lower_bound(moment);
+ if (i != history->prices.begin()) {
+ --i;
age = (*i).first;
price = (*i).second;
- break;
}
+ }
+ }
if (updater)
(*updater)(*this, moment, age,