The tight loop elixir accelerates

I am looking for ways to speed up the dead end in my elixir program.

Enum.reduce( list, 0, fn ({c,v},acc) -> v*elem(tuple_array,c) + acc end ) 

He simply looks at the list of tuples and for each of them: search for a tuple (c is an integer), multiplication and addition.

I tried to insert into the module head

 @compile [:native, {:hipe, [:verbose, :o3]}] 

and on macOS, it's a native compilation. But when I go and run the code from the iex shell it works even slower than before. Did I miss something?

UPDATE May 3, 2015. Now I realized that my hard loop works at speeds almost equivalent to compiled languages ​​like Fortran, rather than orders of magnitude slower. My real bottleneck seems to be sending the output of this loop to another process, especially when it happens between nodes on the network.

Thanks to everyone who showed interest.

+5
source share
1 answer

Have you tried with the template? This is usually faster than using Enum.reduce, which uses lists under the hood: 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 
+1
source

All Articles