Time accuracy within intervals

I have a question about Elixir/Erlang time precision.

defmodule Test do
  use GenServer

  def start_link do
    GenServer.start_link(__MODULE__, [], name: __MODULE__)
  end

  def init(state) do
    next()

    {:ok, state}
  end

  def handle_info(:next, state) do
    spawn_link(fn -> IO.puts :os.system_time(:millisecond) end)
    next()

    {:noreply, state}
  end

  defp next() do
    Process.send_after(self(), :next, 50)
  end
end

The average delay I’m getting between calls is around 62.5ms.

iex(1)> 1609107565704
iex(1)> 1609107565768
iex(1)> 1609107565831
iex(1)> 1609107565893
iex(1)> 1609107565955
iex(1)> 1609107566018
iex(1)> 1609107566081
iex(1)> 1609107566145

I’m running the Test module isolated from the rest of the app on a relatively new piece of hardware. I wonder if there is a way to get more accurate intervals - or if I’m doing something wrong?

I get 51ms difference with your code. This is expected because between receiving a message and starting a new timeout you have computations.

You may want to use :timer.send_interval:

defmodule Test do
  use GenServer

  def start_link do
    GenServer.start_link(__MODULE__, [], name: __MODULE__)
  end

  def init(state) do
    :timer.send_interval(50, :next)

    {:ok, state}
  end

  def handle_info(:next, state) do
    IO.puts :os.system_time(:millisecond)

    {:noreply, state}
  end

end
2 Likes