Closed KJhellico closed 5 months ago
Totals | |
---|---|
Change from base Build 8972750703: | 0.0% |
Covered Lines: | 11064 |
Relevant Lines: | 11064 |
I tried this, but there is no performance gain.
from datetime import date, timedelta
import timeit
class td:
def __init__(self, days: int = 0):
self._days = days
def __radd__(self, other):
if isinstance(other, date):
return date.fromordinal(other.toordinal() + self._days)
return NotImplemented
def __rsub__(self, other):
if isinstance(other, date):
return date.fromordinal(other.toordinal() - self._days)
return NotImplemented
def _start_days(dt: date, n: int) -> date:
return date.fromordinal(dt.toordinal() + n)
def with_timedelta():
dt = date(2024, 1, 1)
d2 = dt + timedelta(days=10)
def with_start_days():
dt = date(2024, 1, 1)
d2 = _start_days(dt, 10)
def with_class_td():
dt = date(2024, 1, 1)
d2 = dt + td(10)
NUM = 10_000_000
t1 = timeit.timeit(with_timedelta, number=NUM)
t2 = timeit.timeit(with_start_days, number=NUM)
t3 = timeit.timeit(with_class_td, number=NUM)
print(f"{NUM} times:")
print(f"timedelta = {t1:.3f}")
print(f"_start_days = {t2:.3f} ({(t2/t1 - 1)*100:.2f}%)")
print(f"class td = {t3:.3f} ({(t3/t1 - 1)*100:.2f}%)")
Result:
10000000 times:
timedelta = 5.534
_start_days = 3.749 (-32.25%)
class td = 5.505 (-0.52%)
Issues
0 New issues
0 Accepted issues
Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code
Proposed change
Replace
datetime::timedelta
with simple custom function for adding and subtracting days.Performance test script:
Result:
Type of change
Checklist
make pre-commit
, it didn't generate any changesmake test
, all tests passed locally