Skip to content

Instantly share code, notes, and snippets.

@nyaray
Created August 6, 2019 16:30
Show Gist options
  • Save nyaray/8dc380c8df9768ac2875c54c2bb6cd20 to your computer and use it in GitHub Desktop.
Save nyaray/8dc380c8df9768ac2875c54c2bb6cd20 to your computer and use it in GitHub Desktop.
paste these functions into a module where you want to profile runtimes
# call this before the code under inspection
defp clock_start(x) do
now = :erlang.timestamp()
Process.put(:"$clock_start", now)
Process.put(:"$clock_step_start", now)
Process.put(:"$clock_steps", [])
x
end
# call this after start when you want to checkpoint intermediate steps
defp cLOCK_STEP(x, tag) do
now = :erlang.timestamp()
step_start = Process.get(:"$clock_step_start")
steps = Process.get(:"$clock_steps", [])
Process.put(:"$clock_steps", [{tag, :timer.now_diff(now, step_start)} | steps])
Process.put(:"$clock_step_start", now)
x
end
# call this to get all accumulated measured steps
defp clock_read(), do: Process.get(:"$clock_steps") |> Enum.reverse()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment