我有了(除其他事项外)类:TreeMap的过滤视图性能
public class TimeSeries {
private final NavigableMap<LocalDate, Double> prices;
public TimeSeries() { prices = new TreeMap<>(); }
private TimeSeries(NavigableMap<LocalDate, Double> prices) {
this.prices = prices;
}
public void add(LocalDate date, double price) { prices.put(date, price); }
public Set<LocalDate> dates() { return prices.keySet(); }
//the 2 methods below are examples of why I need a TreeMap
public double lastPriceAt(LocalDate date) {
Map.Entry<LocalDate, Double> price = prices.floorEntry(date);
return price.getValue(); //after some null checks
}
public TimeSeries between(LocalDate from, LocalDate to) {
return new TimeSeries(this.prices.subMap(from, true, to, true));
}
}
现在我需要的地图,只有部分日期可在“过滤”的看法。该效果我已经添加了以下方法:
public TimeSeries onDates(Set<LocalDate> retainDates) {
TimeSeries filtered = new TimeSeries(new TreeMap<> (this.prices));
filtered.dates().retainAll(retainDates);
return filtered;
}
的onDates
方法是一个巨大的性能瓶颈,代表的程序的处理时间的85%。由于该程序正在运行数百万次的模拟,这意味着花费数小时的时间。
我该如何提高该方法的性能?
过滤的'TimeSeries'副本将迭代多少次? 'retainDates'通常比'prices'小很多? – biziclop 2014-08-29 17:55:35
@biziclop地图通常包含1500个条目,并且该集合将具有几乎相同的大小(可能具有相同的大小,包含相同的日期)。过滤的TimeSeries通常只用于(迭代)一次。 – assylias 2014-08-29 17:58:15
我对LocalDate并不太熟悉,但仅仅通过执行prices.get(localeDate)就可以获得想要的值? – coffeeaddict 2014-08-29 18:00:16