Skip to content

Commit

Permalink
Merge pull request #852.
Browse files Browse the repository at this point in the history
Improve Calendar performance
  • Loading branch information
lballabio committed Jun 29, 2020
2 parents 921d0a6 + bade142 commit 0815aef
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
2 changes: 2 additions & 0 deletions Docs/pages/license.docs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@
Copyright (C) 2019 SoftSolutions! S.r.l.
Copyright (C) 2019 Wojciech Slusarski

Copyright (C) 2020 Kline s.r.l.
Copyright (C) 2020 Leonardo Arcari
Copyright (C) 2020 Piotr Siejda

QuantLib includes code taken from Peter Jäckel's book "Monte Carlo
Expand Down
2 changes: 2 additions & 0 deletions LICENSE.TXT
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ QuantLib is
Copyright (C) 2019 SoftSolutions! S.r.l.
Copyright (C) 2019 Wojciech Slusarski

Copyright (C) 2020 Kline s.r.l.
Copyright (C) 2020 Leonardo Arcari
Copyright (C) 2020 Piotr Siejda

QuantLib includes code taken from Peter J�ckel's book "Monte Carlo
Expand Down
27 changes: 14 additions & 13 deletions ql/time/calendar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
Copyright (C) 2003, 2004, 2005, 2006, 2007 StatPro Italia srl
Copyright (C) 2004 Jeff Yu
Copyright (C) 2014 Paolo Mazzocchi
Copyright (C) 2020 Leonardo Arcari
Copyright (C) 2020 Kline s.r.l.
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
Expand Down Expand Up @@ -70,7 +72,7 @@ namespace QuantLib {
if (c == Following || c == ModifiedFollowing
|| c == HalfMonthModifiedFollowing) {
while (isHoliday(d1))
d1++;
++d1;
if (c == ModifiedFollowing
|| c == HalfMonthModifiedFollowing) {
if (d1.month() != d.month()) {
Expand All @@ -84,16 +86,16 @@ namespace QuantLib {
}
} else if (c == Preceding || c == ModifiedPreceding) {
while (isHoliday(d1))
d1--;
--d1;
if (c == ModifiedPreceding && d1.month() != d.month()) {
return adjust(d,Following);
}
} else if (c == Nearest) {
Date d2 = d;
while (isHoliday(d1) && isHoliday(d2))
{
d1++;
d2--;
++d1;
--d2;
}
if (isHoliday(d1))
return d2;
Expand All @@ -116,17 +118,17 @@ namespace QuantLib {
Date d1 = d;
if (n > 0) {
while (n > 0) {
d1++;
++d1;
while (isHoliday(d1))
d1++;
n--;
++d1;
--n;
}
} else {
while (n < 0) {
d1--;
--d1;
while(isHoliday(d1))
d1--;
n++;
--d1;
++n;
}
}
return d1;
Expand Down Expand Up @@ -176,9 +178,9 @@ namespace QuantLib {
}

if (isBusinessDay(from) && !includeFirst)
wd--;
--wd;
if (isBusinessDay(to) && !includeLast)
wd--;
--wd;

if (from > to)
wd = -wd;
Expand Down Expand Up @@ -317,5 +319,4 @@ namespace QuantLib {
}
return result;
}

}
11 changes: 9 additions & 2 deletions ql/time/calendar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl
Copyright (C) 2003, 2004, 2005, 2006, 2007 StatPro Italia srl
Copyright (C) 2006 Piter Dias
Copyright (C) 2020 Leonardo Arcari
Copyright (C) 2020 Kline s.r.l.
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
Expand Down Expand Up @@ -216,11 +218,13 @@ namespace QuantLib {

inline const std::set<Date>& Calendar::addedHolidays() const {
QL_REQUIRE(impl_, "no calendar implementation provided");

return impl_->addedHolidays;
}

inline const std::set<Date>& Calendar::removedHolidays() const {
QL_REQUIRE(impl_, "no calendar implementation provided");

return impl_->removedHolidays;
}

Expand All @@ -233,9 +237,12 @@ namespace QuantLib {
const Date& _d = d;
#endif

if (impl_->addedHolidays.find(_d) != impl_->addedHolidays.end())
if (!impl_->addedHolidays.empty() &&
impl_->addedHolidays.find(_d) != impl_->addedHolidays.end())
return false;
if (impl_->removedHolidays.find(_d) != impl_->removedHolidays.end())

if (!impl_->removedHolidays.empty() &&
impl_->removedHolidays.find(_d) != impl_->removedHolidays.end())
return true;

return impl_->isBusinessDay(_d);
Expand Down

0 comments on commit 0815aef

Please sign in to comment.