Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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