Skip to content

Commit 1e12c7d

Browse files
authored
Optimize Duration.add, subtract, multiply, negate (#15501)
1 parent 264200a commit 1e12c7d

1 file changed

Lines changed: 40 additions & 36 deletions

File tree

lib/elixir/lib/calendar/duration.ex

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -243,19 +243,19 @@ defmodule Duration do
243243
244244
"""
245245
@spec add(t, t) :: t
246-
def add(%Duration{} = d1, %Duration{} = d2) do
247-
{m1, p1} = d1.microsecond
248-
{m2, p2} = d2.microsecond
246+
def add(%Duration{microsecond: {ms1, p1}} = d1, %Duration{microsecond: {ms2, p2}} = d2) do
247+
%{year: y1, month: mo1, week: w1, day: day1, hour: h1, minute: mi1, second: s1} = d1
248+
%{year: y2, month: mo2, week: w2, day: day2, hour: h2, minute: mi2, second: s2} = d2
249249

250250
%Duration{
251-
year: d1.year + d2.year,
252-
month: d1.month + d2.month,
253-
week: d1.week + d2.week,
254-
day: d1.day + d2.day,
255-
hour: d1.hour + d2.hour,
256-
minute: d1.minute + d2.minute,
257-
second: d1.second + d2.second,
258-
microsecond: {m1 + m2, max(p1, p2)}
251+
year: y1 + y2,
252+
month: mo1 + mo2,
253+
week: w1 + w2,
254+
day: day1 + day2,
255+
hour: h1 + h2,
256+
minute: mi1 + mi2,
257+
second: s1 + s2,
258+
microsecond: {ms1 + ms2, max(p1, p2)}
259259
}
260260
end
261261

@@ -273,19 +273,19 @@ defmodule Duration do
273273
274274
"""
275275
@spec subtract(t, t) :: t
276-
def subtract(%Duration{} = d1, %Duration{} = d2) do
277-
{m1, p1} = d1.microsecond
278-
{m2, p2} = d2.microsecond
276+
def subtract(%Duration{microsecond: {ms1, p1}} = d1, %Duration{microsecond: {ms2, p2}} = d2) do
277+
%{year: y1, month: mo1, week: w1, day: day1, hour: h1, minute: mi1, second: s1} = d1
278+
%{year: y2, month: mo2, week: w2, day: day2, hour: h2, minute: mi2, second: s2} = d2
279279

280280
%Duration{
281-
year: d1.year - d2.year,
282-
month: d1.month - d2.month,
283-
week: d1.week - d2.week,
284-
day: d1.day - d2.day,
285-
hour: d1.hour - d2.hour,
286-
minute: d1.minute - d2.minute,
287-
second: d1.second - d2.second,
288-
microsecond: {m1 - m2, max(p1, p2)}
281+
year: y1 - y2,
282+
month: mo1 - mo2,
283+
week: w1 - w2,
284+
day: day1 - day2,
285+
hour: h1 - h2,
286+
minute: mi1 - mi2,
287+
second: s1 - s2,
288+
microsecond: {ms1 - ms2, max(p1, p2)}
289289
}
290290
end
291291

@@ -302,14 +302,16 @@ defmodule Duration do
302302
"""
303303
@spec multiply(t, integer) :: t
304304
def multiply(%Duration{microsecond: {ms, p}} = duration, integer) when is_integer(integer) do
305+
%{year: y, month: mo, week: w, day: d, hour: h, minute: mi, second: s} = duration
306+
305307
%Duration{
306-
year: duration.year * integer,
307-
month: duration.month * integer,
308-
week: duration.week * integer,
309-
day: duration.day * integer,
310-
hour: duration.hour * integer,
311-
minute: duration.minute * integer,
312-
second: duration.second * integer,
308+
year: y * integer,
309+
month: mo * integer,
310+
week: w * integer,
311+
day: d * integer,
312+
hour: h * integer,
313+
minute: mi * integer,
314+
second: s * integer,
313315
microsecond: {ms * integer, p}
314316
}
315317
end
@@ -327,14 +329,16 @@ defmodule Duration do
327329
"""
328330
@spec negate(t) :: t
329331
def negate(%Duration{microsecond: {ms, p}} = duration) do
332+
%{year: y, month: mo, week: w, day: d, hour: h, minute: mi, second: s} = duration
333+
330334
%Duration{
331-
year: -duration.year,
332-
month: -duration.month,
333-
week: -duration.week,
334-
day: -duration.day,
335-
hour: -duration.hour,
336-
minute: -duration.minute,
337-
second: -duration.second,
335+
year: -y,
336+
month: -mo,
337+
week: -w,
338+
day: -d,
339+
hour: -h,
340+
minute: -mi,
341+
second: -s,
338342
microsecond: {-ms, p}
339343
}
340344
end

0 commit comments

Comments
 (0)