Please note that your solution has a very bad performance, hence I'm not upvoting it for best practices or clever. The ++ operater is VERY expensive in Elixir, please compare your result with mine to see how list prepending can tremendously increase your performance.
Just to illustrate how not performant this is, here are several examples:
n = 1 000 | Yours : 00.0080, Mine: 00.0031
n = 10 000 | Yours : 00.0794, Mine: 00.0338 (so far just double more or less, okay i guess. Now you'll see the impact.)
n = 100 000 | Yours : 12.3896, Mine: 00.1321
n = 500 000 | Yours : 604.644, Mine: 00.9541 (not going higher than this)
This illustrates that with big data, time spent upon your function is a lot longer. Hopefully people will read this comment and realize that performance is definitely something to keep in mind! :)
As for the code to measure this:
defmodule Benchmark do
def measure(function) do
function
|> :timer.tc()
|> elem(0)
|> Kernel./(1_000_000)
end
end
defmodule Random do
def random_array(length, list \\ [])
def random_array(0, list), do: list
def random_array(length, list), do: random_array(length - 1, [random_number() | list])
def random_number(), do: :rand.uniform(1000)
end
n = Random.random_array 10000
t1 = Benchmark.measure fn -> Makevalley.make_valley(n) end
t2 = Benchmark.measure fn -> Makevalley.make_valley2(n) end
Beware of big numbers, tail-call optimization is not used here! For more information about Elixir tail-call optimization, read the official docs regarding recursion https://elixir-lang.org/getting-started/recursion.html where (at the moment of writing) is a link to the wikipedia "tail-call" page.
Beware of big numbers, tail-call optimization is not used here! For more information about Elixir tail-call optimization, read the official docs regarding recursion https://elixir-lang.org/getting-started/recursion.html where (at the moment of writing) is a link to the wikipedia "tail-call" page.
Please note that your solution has a very bad performance, hence I'm not upvoting it for best practices or clever. The ++ operater is VERY expensive in Elixir, please compare your result with mine to see how list prepending can tremendously increase your performance.
Just to illustrate how not performant this is, here are several examples:
This illustrates that with big data, time spent upon your function is a lot longer. Hopefully people will read this comment and realize that performance is definitely something to keep in mind! :)
As for the code to measure this:
Beware of big numbers, tail-call optimization is not used here! For more information about Elixir tail-call optimization, read the official docs regarding recursion https://elixir-lang.org/getting-started/recursion.html where (at the moment of writing) is a link to the wikipedia "tail-call" page.
Beware of big numbers, tail-call optimization is not used here! For more information about Elixir tail-call optimization, read the official docs regarding recursion https://elixir-lang.org/getting-started/recursion.html where (at the moment of writing) is a link to the wikipedia "tail-call" page.