Running File.ls many times and seeing how fast it was gave me an idea. So I 
wrote this code and benchmarked it.


 
defmodule Benchmark do

  def run(function) do

    function |> :timer.tc() |> elem(0) |> Kernel./(1_000_000)

  end

end

defmodule MyFile do

  def list(directory, filter) do

    Enum.reduce([directory], [], fn item, acc ->

      if String.ends_with?(item, filter) do

        [item] ++ acc

      else

        item

        |> File.ls()

        |> case do

          {:ok, files} -> Enum.reduce(files, [], &(item |> Path.join(&1) |> 
list(filter) |> Kernel.++(&2))) ++ acc

          _ -> acc

        end

      end

    end)

  end

end




*Docker Results*


iex(45)> Benchmark.run(fn -> Path.wildcard("lib/**/*.ex") end) 

0.877545 

iex(46)> Benchmark.run(fn -> Enum.each(0..10, fn _ -> Path.wildcard(
"lib/**/*.ex") end) end) 

9.808442 

iex(47)> Benchmark.run(fn -> MyFile.list("lib", ".ex") end) 

0.380802 

iex(48)> Benchmark.run(fn -> Enum.each(0..10, fn _ -> MyFile.list("lib", 
".ex") end) end) 

4.368466



*Mac Results*


iex(29)> Benchmark.run(fn -> Path.wildcard("lib/**/*.ex") end)

0.053149

iex(30)> Benchmark.run(fn -> Enum.each(0..10, fn _ -> Path.wildcard(
"lib/**/*.ex") end) end)

0.641741

iex(31)> Benchmark.run(fn -> MyFile.list("lib", ".ex") end)

0.019919

iex(32)> Benchmark.run(fn -> Enum.each(0..10, fn _ -> MyFile.list("lib", 
".ex") end) end)

0.248769


-- 
You received this message because you are subscribed to the Google Groups 
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elixir-lang-core+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elixir-lang-core/53169987-cb9f-4f02-9709-c8766ec9830e%40googlegroups.com.

Reply via email to