Skip to content

Instantly share code, notes, and snippets.

@keathley
Created January 8, 2020 16:31
Show Gist options
  • Save keathley/30ded3c0b95ed8282880e9537fa85a3c to your computer and use it in GitHub Desktop.
Save keathley/30ded3c0b95ed8282880e9537fa85a3c to your computer and use it in GitHub Desktop.
defmodule PCache do
def start do
# data = for i <- 0..1_000, do: {i, i}, into: %{}
spawn(fn ->
accept_reads()
end)
end
def get(pid) do
us = self()
send(pid, {:get, us})
receive do
val ->
val
end
end
def time do
pid = start()
{time, _} = :timer.tc(fn -> get(pid) end)
time
end
def accept_reads() do
receive do
{:get, them} ->
send(them, 42)
accept_reads()
end
end
end
defmodule EtsCache do
def start do
:ets.new(:test, [:named_table, :public, :set])
:ets.insert(:test, {:get, 42})
end
def get(tab) do
case :ets.lookup(tab, :get) do
[{:get, val}] -> val
[] -> nil
end
end
def time do
start()
{time, _} = :timer.tc(fn -> get(:test) end)
time
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment