rerdos

rerdos

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?

Marked As Solved

lud

lud

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

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New

We're in Beta

About us Mission Statement