Last active
January 22, 2016 18:23
-
-
Save rwdaigle/9893b3e213f3c993e792 to your computer and use it in GitHub Desktop.
Parallel processing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Worker do | |
def work(num) do | |
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat." | |
|> String.split | |
|> Enum.map(&String.upcase/1) | |
|> Enum.join | |
end | |
end | |
iterations = 100000 | |
{us, :ok} = :timer.tc fn -> | |
0..iterations |> | |
Stream.map(&Task.async(Worker, :work, [&1])) |> | |
Enum.each(&Task.await(&1)) | |
end | |
IO.puts "Time executing #{iterations} jobs in parallel: #{us/1000}ms" | |
{us, :ok} = :timer.tc fn -> | |
0..iterations |> | |
Enum.each(&Worker.work(&1)) | |
end | |
IO.puts "Time executing #{iterations} jobs sequentially: #{us/1000}ms" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In this version, sequential is, indeed faster: