# Time accuracy within intervals

**URL:** <https://forum.elixirforum.com/t/time-accuracy-within-intervals/36465>\
**Category:** Questions\
**Tags:** erlang, time\
**Created:** [December 27, 2020, 10:47pm UTC](https://forum.elixirforum.com/t/time-accuracy-within-intervals/36465 "2020-12-27T22:47:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rerdos](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/rerdos/32/21271_2.png) [@rerdos](https://forum.elixirforum.com/u/rerdos)\
**Post date:** [December 27, 2020, 10:47pm UTC](https://forum.elixirforum.com/t/time-accuracy-within-intervals/36465/1 "2020-12-27T22:47:44Z")

</div>

I have a question about Elixir/Erlang time precision.

```elixir
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.

```elixir
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?

---

<div class="post-metadata">

**Author:** ![lud](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lud/32/14382_2.png) [@lud](https://forum.elixirforum.com/u/lud)\
**Post date:** [December 28, 2020, 5:28pm UTC](https://forum.elixirforum.com/t/time-accuracy-within-intervals/36465/2 "2020-12-28T17:28:48Z")

</div>

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`:

```elixir
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

```
