Skip to content

Instantly share code, notes, and snippets.

@alco
Created October 9, 2013 13:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alco/6901138 to your computer and use it in GitHub Desktop.
Save alco/6901138 to your computer and use it in GitHub Desktop.
λ elixir --erl '+P 2000000' -r spawn.ex -e 'Spawner.run(1000000)'
Memory after spawn [total: 2810825464, processes: 2772817683, processes_used: 2772817636, system: 38007781, atom: 331249, atom_used: 300914, binary: 1469240, code: 7652696, ets: 412072]
Time taken to spawn: 4.690528 s
Time to collect results: 5.752107 s
Memory in the end [total: 73518240, processes: 59105061, processes_used: 53168996, system: 14413179, atom: 331249, atom_used: 302425, binary: 1507208, code: 7802697, ets: 424408]
End result = 1000000
defmodule Spawner do
def run(count) do
run(count, count, :erlang.now)
end
def run(0, nroutines, tstamp) do
IO.puts "Memory after spawn #{inspect :erlang.memory}"
time = :erlang.now
IO.puts "Time taken to spawn: #{ :timer.now_diff(time, tstamp)/1000000 } s"
collect_results(nroutines, 0, time)
end
def run(count, nroutines, t) do
pid = self()
spawn(fn -> pid <- {self, 1}; receive do _ -> :ok end end)
run(count-1, nroutines, t)
end
def collect_results(0, total, t) do
time = :erlang.now
IO.puts "Time to collect results: #{ :timer.now_diff(time, t)/1000000 } s"
:erlang.garbage_collect
IO.puts "Memory in the end #{inspect :erlang.memory}"
IO.puts "End result = #{total}"
end
def collect_results(nroutines, total, t) do
receive do
{pid, x} ->
pid <- :ok
collect_results(nroutines-1, total+x, t)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment