5

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.

GavinBrelstaff
  • 3,016
  • 2
  • 21
  • 39
  • okay may be the answer is to use an Erlang Native Implemented Function for this bit of code - using gcc to compile it. Let's see ... – GavinBrelstaff Apr 20 '15 at 13:35

1 Answers1

1

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
rorra
  • 9,593
  • 3
  • 39
  • 61
  • Thanks rorra, but get no appreciable speed up using your module. I guess Enum.reduce is doing a good optimisation to Erlang already. – GavinBrelstaff Apr 21 '15 at 19:41