Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance 40x slower for comprehension operation in IEx #5073

Closed
sunboshan opened this issue Jul 28, 2016 · 10 comments
Closed

Performance 40x slower for comprehension operation in IEx #5073

sunboshan opened this issue Jul 28, 2016 · 10 comments

Comments

@sunboshan
Copy link
Contributor

sunboshan commented Jul 28, 2016

Environment

  • Elixir version (elixir -v):
Erlang/OTP 19 [erts-8.0.2] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]

Elixir 1.3.2
  • Operating system: Mac OSX 10.11

Here's a piece of code computing pythagorean triples using comprehension.

defmodule Foo do
  def bar do
    triplet = fn ->
      for i <- 1..100, j <- i..100, k <- j..100, i * i + j * j == k * k, i + j + k == 180, do: [i, j, k]
    end
    :timer.tc(triplet) |> elem(0) |> Kernel./(1_000_000)
  end
end

Foo.bar |> IO.inspect

Run it

$ elixir temp.exs
0.031906

Exact same piece of code in IEx

iex> triplet = fn -> for i <- 1..100, j <- i..100, k <- j..100, i * i + j * j == k * k, i + j + k == 180, do: [i, j, k] end
iex> :timer.tc(triplet) |> elem(0) |> Kernel./(1_000_000)
1.231746

It's 40x slower than running through an .exs file. Not sure if it's a bug in IEx or it's expected to be slower in IEx. Any thoughts?

@josevalim
Copy link
Member

In IEx code is evaluated and not compiled, so it is expected. Never benchmark outside of a compiled module.

@sunboshan
Copy link
Contributor Author

sunboshan commented Jul 28, 2016

@josevalim Thanks for the explanation! I fount it in the IEx doc. But isn't that .exs is just interpreted not compiled?

@josevalim
Copy link
Member

.exs is also compiled but the bytecode modules is not written to disk (only loaded in memory).

@sunboshan
Copy link
Contributor Author

Awesome! I was under impression that .exs is not compiled after reading Dave's Elixir book.

These files will typically have the extension .ex or .exs. This is a convention -- files ending in .ex are intended to be compiled into bytecodes and then run, whereas those ending in .exs are more like programs in scripting languages -- they are effectively interpreted at the source level.

@josevalim
Copy link
Member

Please submit an errata because that's not correct. :)

On Thursday, July 28, 2016, sunboshan notifications@github.com wrote:

Awesome! I was under impression that .exs is not compiled after reading
Dave's Elixir book.

These files will typically have the extension .ex or .exs. This is a
convention -- files ending in .ex are intended to be compiled into
bytecodes and then run, whereas those ending in .exs are more like programs
in scripting languages -- they are effectively interpreted at the source
level.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#5073 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAAlbheOUUNhoJnUeqRwZ_TdKpMvbHpvks5qaNDRgaJpZM4JW3rJ
.

José Valimwww.plataformatec.com.br
http://www.plataformatec.com.br/Founder and Director of R&D

@ericmj
Copy link
Member

ericmj commented Jul 28, 2016

To clarify; Elixir does not care about the file ending, Elixir will do the same thing regardless if the file ends in .ex, .exs or .rb. There is a convention though that .ex files are supposed to produce compiled .beam files to disk and .exs should only load modules into memory. But this is only a convention, you still need to call the different commands elixir, elixirc or iex to get the different behaviours.

@k-solutions
Copy link

My opinion is that should be clearly explained in docs, as I saw few
benchmarks on Plug from iex itself with weak results.

On Jul 28, 2016 8:36 PM, "Eric Meadows-Jönsson" notifications@github.com
wrote:

To clarify; Elixir does not care about the file ending, Elixir will do the
same thing regardless if the file ends in .ex, .exs or .rb. There is a
convention though that .ex files are supposed to produce compiled .beam
files to disk and .exs should only load modules into memory. But this is
only a convention, you still need to call the different commands elixir,
elixirc or iex to get the different behaviours.


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#5073 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAOrfabdiJMBPmcxXa5FAkSDtmIwfLXWks5qaOiVgaJpZM4JW3rJ
.

@ericmj
Copy link
Member

ericmj commented Jul 28, 2016

@k-solutions We are also in the opinion that things should be explained in documentation. It's currently documented here: http://elixir-lang.org/getting-started/modules.html#scripted-mode, it's also the first result when I google "elixir exs". If you think the documentation can be improved a PR would be very appreciated. External contributions are always appreciated for documentation since the core developers usually already know the subject and are, hence, not as good at knowing what is difficult or hard to find.

Benchmarking from iex is not necessarily bad if you make sure to call compiled code.

@sunboshan
Copy link
Contributor Author

@ericmj Thanks for above link. However in that doc, it is said

Elixir treats both files exactly the same way, the only difference is in intention. .ex files are meant to be compiled while .exs files are used for scripting, without the need for compilation.

Per discussion with @josevalim above, .exs is also compiled in memory, it's just didn't write any bytecode file in disk. The sentence without the need for compilation is somehow making the reader to think .exs doesn't need compile.

@ericmj
Copy link
Member

ericmj commented Jul 28, 2016

While docs are correct I can see how they are confusing. "Without the need for compilation" means that they don't need to be compiled but there can still be compiled elements in them. An .ex file OTOH needs to be compiled or they would have no effect.

A PR would be very appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants