# Performance of Dates.Time equality and comparison

**URL:** <https://discourse.julialang.org/t/performance-of-dates-time-equality-and-comparison/28738>\
**Category:** Performance\
**Tags:** question\
**Created:** [September 13, 2019, 8:24pm UTC](https://discourse.julialang.org/t/performance-of-dates-time-equality-and-comparison/28738 "2019-09-13T20:24:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![artemsolod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/artemsolod/32/20704_2.png) [@artemsolod](https://discourse.julialang.org/u/artemsolod)\
**Post date:** [September 13, 2019, 8:24pm UTC](https://discourse.julialang.org/t/performance-of-dates-time-equality-and-comparison/28738/1 "2019-09-13T20:24:36Z")

</div>

I am noticing large performance costs when doing operations with `Time` (as compared to `DateTime`). One example would be

```julia
using Dates
n = now();
arr_dt = [n for _ in 1:10^7];
arr_ts = Time.(arr_dt);

all(arr_dt .== arr_dt); all(arr_ts .== arr_ts);
@time all(arr_dt .== arr_dt)
@time all(arr_ts .== arr_ts)

```

On Julia-1.1.1 yields the following

```julia
  0.011878 seconds (10 allocations: 1.197 MiB)
  1.261891 seconds (10 allocations: 1.197 MiB)

```

From `Dates.Time` sources I understand that equality is implemented by comparing hours, minutes, etc components. What is the rationale for this approach when `Time` itself wraps a single `Int` of nanoseconds? Wouldn’t modulo arithmetic suffice in this design? What would you advice to use to have faster time comparisons? Thanks!

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [September 14, 2019, 3:29am UTC](https://discourse.julialang.org/t/performance-of-dates-time-equality-and-comparison/28738/2 "2019-09-14T03:29:50Z")

</div>

It appears you are benchmarking in the global scope. If so, you are very unlikely to see meaningful results.

---

<div class="post-metadata">

**Author:** ![artemsolod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/artemsolod/32/20704_2.png) [@artemsolod](https://discourse.julialang.org/u/artemsolod)\
**Post date:** [September 14, 2019, 9:35am UTC](https://discourse.julialang.org/t/performance-of-dates-time-equality-and-comparison/28738/3 "2019-09-14T09:35:52Z")

</div>

My bad, thank you for pointing this out. However, with code wrapped in functions (attached below) results are pretty much the same, so my initial questions stand.

```julia
using Dates
n = now();
arr_dt = [n for _ in 1:10^7];
arr_ts = Time.(arr_dt);
f_dt() = all(arr_dt .== arr_dt)
f_ts() = all(arr_ts .== arr_ts)

f_dt(); f_ts();
@time f_dt();
@time f_ts();

```

```julia
  0.011563 seconds (10 allocations: 1.197 MiB)
  1.271418 seconds (10 allocations: 1.197 MiB, 3.69% gc time)

```

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [September 14, 2019, 10:09am UTC](https://discourse.julialang.org/t/performance-of-dates-time-equality-and-comparison/28738/4 "2019-09-14T10:09:45Z")

</div>

> [@artemsolod](#):
>
> What is the rationale for this approach when `Time` itself wraps a single `Int` of nanoseconds? Wouldn’t modulo arithmetic suffice in this design? What would you advice to use to have faster time comparisons? Thanks!

It might very well be that this case could be optimized. If you care about the performance of this you could try to implement a faster `==` for `DateTime` add some extra test and make a PR to Julia with it. Then everyone will benefit from it, instead of just having a local workaround.
