salman

salman

Why is the chain.exs example of creating 1m processes slower on my faster laptop?

In chapter 15 of Dave T. book programming elixir, there is an example chain.exs that basically creates many processes, each one incrementing the value and passing value as a message to another process.

Anyhow Dave mentions that he runs this code on his 2011 macbook air 2.13GHz core 2 dua with only 4GB of ram.

I ran the same code on my macbook pro (2016) and for some reason my code is running slow than his. Mine is a Macbook pro 15" 2.6Ghz i7, 16GB ram.

elixir --erl “+P 1000000” -r chain.exs -e “Chain.run(1_000_000)”

{10799103, “Result is 1000000”}

His results were:

{5135238, “Result is 1000000}” # page # 193 in his book

Any ideas on why it would be running slower on my laptop? Its 10 seconds compared to his 5 seconds.

The code that I am running is:

defmodule Chain do
  def counter(next_pid) do
    receive do
      n -> send next_pid, n + 1
    end
  end

  def create_processes(n) do
    last = Enum.reduce 1..n, self(), fn (_, send_to) -> spawn(Chain, :counter, [send_to]) end
    send last, 0

    receive do
      final_answer when is_integer(final_answer) ->
        "Result is #{inspect(final_answer)}"
    end
  end

  def run(n) do
    IO.puts inspect :timer.tc(Chain, :create_processes, [n])
  end
end

Most Liked

peerreynders

peerreynders

  Model Name:	MacBook Pro
  Model Identifier:	MacBookPro13,3
  Processor Name:	Intel Core i7
  Processor Speed:	2.9 GHz
  Number of Processors:	1
  Total Number of Cores:	4
  L2 Cache (per Core):	256 KB
  L3 Cache:	8 MB
  Memory:	16 GB
  System Version:	macOS 10.13.5 (17F77)
  Kernel Version:	Darwin 17.6.0
$ elixir -v
Erlang/OTP 21 [erts-10.1.1] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [hipe] [dtrace]

Elixir 1.7.4 (compiled with Erlang/OTP 21)
# 8 schedulers (online) - 262144 processes
$ elixir -r chain.exs -e "Chain.run(10)"
{30, "Result is 10"}
# vs. {4015, "Result is 10"}
$ elixir -r chain.exs -e "Chain.run(100)"
{1013, "Result is 100"}
# vs. {4562, "Result is 100"}
$ elixir -r chain.exs -e "Chain.run(1000)"
{7418, "Result is 1000"}
# vs. {8458, "Result is 1000"}
$ elixir -r chain.exs -e "Chain.run(10_000)"
{88654, "Result is 10000"}
# vs. {66769, "Result is 10000"}

i.e. it is faster if you stay under a 1000 :expressionless:

Tada!

$ elixir --erl "+S 1 +P 1000000" -r chain.exs -e "Chain.run(1_000_000)"
{3251305, "Result is 1000000"}

It is faster by forcing it to a single scheduler.

peerreynders

peerreynders

I didn’t - that was a comment - but I was running this code:

defmodule Chain do
  def counter(next_pid) do
    receive do
      n -> send next_pid, n + 1
    end
  end

  defp reducer(_, send_to),
    do: spawn(Chain, :counter, [send_to])

  def create_processes(n) do
    last = Enum.reduce(1..n, self(), &reducer/2)
    send last, 0

    receive do
      final_answer when is_integer(final_answer) ->
        "Result is #{inspect(final_answer)}"
    end
  end

  def run(n) do
    IO.inspect :erlang.system_info(:schedulers)
    IO.inspect :erlang.system_info(:schedulers_online)
    IO.inspect :erlang.system_info(:process_limit)
    IO.puts inspect :timer.tc(Chain, :create_processes, [n])
  end
end

Where Next?

Popular in Questions Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
Kagamiiiii
Student & New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

We're in Beta

About us Mission Statement