optimization questions
Bjorn Gustavsson
bjorn@REDACTED
Tue May 9 10:00:29 CEST 2006
Gaspar Chilingarov <nm@REDACTED> writes:
> Hello!
>
>
> Can erlang vm's gurus tell me which case is more efficient
>
>
> R = size(Binary),
>
> [ SomeFunction(R, X) || X <- lists:seq(1, 1000) ]
>
> or
>
> [ SomeFunction(size(Binary), X) || X <- lists:seq(1, 1000) ]
>
>
> so - does compiler and vm understand that some parts of recursion are
> constant and move their calculation outside of recursion or I should do
> it by hand?
>
The Beam compiler does not attempt to move operations out of loops,
so your first example is probably more efficient.
Of course, if you really care about performance, you should write your
own tail-recursive function to avoiding constructing a 1000 element list.
> Another issue - what if size(BInary) changed to ... say self() - I mean
> that fetching binar sze may be faster operation than creating variable
> and accessign it every time.
self() is specially optimized by the Beam loader/runtime system - it is
translated to a special instruction. It would probably be better to call
self() directly every time than to have it stored in a variable.
If performance really is important, you must do measurements.
/Bjorn
--
Björn Gustavsson, Erlang/OTP, Ericsson AB
More information about the erlang-questions
mailing list