I'm looking for ways to speed up the "tight-loop" in my elixir program.
Enum.reduce( list, 0, fn ({c,v},acc) -> v*elem(tuple_array,c) + acc end )
It's simply running through a list of tuples and for each it's doing: a tuple lookup (c is an integer), a multiplication, and an addition.
I tried inserting in the module's head
@compile [:native, {:hipe, [:verbose, :o3]}]
and on macOS it shows it compiling native. Yet when I go and run the code from the iex shell it runs even slower than before. Am I missing something here?
UPDATE 3 May 2015 I have now come to realize that my tight-loop is running at speeds nearly equivalent to compiled languages like Fortran - well not orders of magnitude slower. My real bottleneck seems to be the sending of the output of that loop to another process - especially when this happens between nodes on a network.
Thanks to all who have shown interest.
Have you tried with pattern matching? Usually its faster than using Enum.reduce which under the hood uses lists:foldr
defmodule Test do
def reduce_list([], acc, f) do
acc
end
def reduce_list([h|t], acc, f) do
reduce_list(t, f.(h, acc), f)
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With