Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elixir tight-loop speed up

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.

like image 256
GavinBrelstaff Avatar asked Nov 10 '22 15:11

GavinBrelstaff


1 Answers

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
like image 117
rorra Avatar answered Nov 15 '22 08:11

rorra