Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions bench/naive_decode.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule Bench do
@epoch_gregorian_seconds 62_167_219_200

def via_unix(seconds) do
seconds
|> DateTime.from_unix!()
|> DateTime.to_naive()
end

def via_gregorian(seconds) do
NaiveDateTime.from_gregorian_seconds(seconds + @epoch_gregorian_seconds)
end
end

Benchee.run(%{
"via_unix" => fn -> Enum.each(1..1_000_000, &Bench.via_unix/1) end,
"via_gregorian" => fn -> Enum.each(1..1_000_000, &Bench.via_gregorian/1) end
})

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Operating System: macOS
CPU Information: Apple M2
Number of Available Cores: 8
Available memory: 8 GB
Elixir 1.19.5
Erlang 28.3
JIT enabled: true

Benchmark suite executing with the following configuration:
warmup: 2 s
time: 5 s
memory time: 0 ns
reduction time: 0 ns
parallel: 1
inputs: none specified
Estimated total run time: 14 s
Excluding outliers: false

Benchmarking via_gregorian ...
Benchmarking via_unix ...
Calculating statistics...
Formatting results...

Name                    ips        average  deviation         median         99th %
via_gregorian         22.29       44.86 ms     ±4.34%       44.25 ms       49.10 ms
via_unix               8.87      112.78 ms     ±1.92%      112.30 ms      116.65 ms

Comparison: 
via_gregorian         22.29
via_unix               8.87 - 2.51x slower +67.92 ms

8 changes: 3 additions & 5 deletions lib/ch/row_binary.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1452,13 +1452,11 @@ defmodule Ch.RowBinary do
{:datetime, timezone} ->
case bin do
<<s::32-little, bin::bytes>> ->
dt = DateTime.from_unix!(s)

dt =
case timezone do
nil -> DateTime.to_naive(dt)
"UTC" -> dt
_ -> DateTime.shift_zone!(dt, timezone)
nil -> NaiveDateTime.from_gregorian_seconds(s + @epoch_gregorian_seconds)
"UTC" -> DateTime.from_unix!(s)
_ -> s |> DateTime.from_unix!() |> DateTime.shift_zone!(timezone)
end

decode_rows(types_rest, bin, [dt | row], rows, types)
Expand Down
Loading