`
ZacMa
  • 浏览: 39116 次
  • 来自: 深圳
社区版块
存档分类
最新评论

<17>Efficiency Guide之Function

阅读更多
1. 函数模式匹配
模式匹配,在函数头,case和receive从句都会被编译器优化,有一些优化排序后也没有好处,例如二进制的匹配。还有如下例子
DO NOT
atom_map1(one) -> 1;
atom_map1(two) -> 2;
atom_map1(three) -> 3;
atom_map1(Int) when is_integer(Int) -> Int;
atom_map1(four) -> 4;
atom_map1(five) -> 5;
atom_map1(six) -> 6.
DO

atom_map2(one) -> 1;
atom_map2(two) -> 2;
atom_map2(three) -> 3;
atom_map2(four) -> 4;
atom_map2(five) -> 5;
atom_map2(six) -> 6;
atom_map2(Int) when is_integer(Int) -> Int.
or

DO

atom_map3(Int) when is_integer(Int) -> Int;
atom_map3(one) -> 1;
atom_map3(two) -> 2;
atom_map3(three) -> 3;
atom_map3(four) -> 4;
atom_map3(five) -> 5;
atom_map3(six) -> 6.
因为第一种,如果one,two,three没有匹配上,那么编译器,总会匹配下一个,如果是four,five,six经过编译器优化后则不会匹配atom_map3(Int) when is_integer(Int) -> Int;

另一个例子是这样,
DO NOT

map_pairs1(_Map, [], Ys) ->
    Ys;
map_pairs1(_Map, Xs, [] ) ->
    Xs;
map_pairs1(Map, [X|Xs], [Y|Ys]) ->
    [Map(X, Y)|map_pairs1(Map, Xs, Ys)].

DO

map_pairs2(_Map, [], Ys) ->
    Ys;
map_pairs2(_Map, [_|_]=Xs, [] ) ->
    Xs;
map_pairs2(Map, [X|Xs], [Y|Ys]) ->
    [Map(X, Y)|map_pairs2(Map, Xs, Ys)].
the compiler is free to rearrange the clauses. It will generate code similar to this
要注意的是,这两种写法,就有及其细微的差别,Xs用[_|_] = Xs代替,仅此而已,这样,编译器可以进行优化为下面的写法
explicit_map_pairs(Map, Xs0, Ys0) ->
    case Xs0 of
[X|Xs] ->
    case Ys0 of
[Y|Ys] ->
    [Map(X, Y)|explicit_map_pairs(Map, Xs, Ys)];
[] ->
    Xs0
    end;
[] ->
    Ys0
    end.
这样的写法,如果参数非空或者很短,则比第一种写法更快

2.  关于函数的调用
(1) Calls to local or external functions (foo(), m:foo()) are the fastest kind of calls.
(2) Calling or applying a fun (Fun(), apply(Fun, [])) is about three times as expensive as calling a local function.
(这里的Mod:Name(),和apply(Mod,Name, []),应该是指自定义函数和用apply调用的函数)
(3) Applying an exported function (Mod:Name(), apply(Mod, Name, [])) is about twice as expensive as calling a fun, or about six times as expensive as calling a local function.
(这里是用apply的方法,调用外部函数,或用apply调用者自定义函数)
尽量多直接调函数,而不是用Fun, 或者apply,不过编译器会将apply(M, F, A)重新写为 M:F(A)

3  关于内存,erlang中所有的循环都来自递归,掌握递归至关重要,为了内存考虑,尽量用尾递归而不是一般的递归
分享到:
评论

相关推荐

    Visual C++ 编程资源大全(英文源码 DLL)

    modify and extract zip archives(92KB)&lt;END&gt;&lt;br&gt;48,zip2.zip&lt;br&gt;The library to create, modify and extract zip archives (31KB)&lt;END&gt;&lt;br&gt;49,pointers.zip&lt;br&gt;A Beginner's Guide to Pointers&lt;br&gt;An article ...

    JQuery In Action.PDF

    The `$("&lt;element&gt;")` function allows developers to generate new elements dynamically. - **Example:** `var newDiv = $("&lt;div&gt;").text("Hello World!");` - **Extending JQuery:** One of the most ...

    User's Guide for NPSOL

    This report forms the user's guide for Version 4.0 of NPSOL, a set of Fortran subroutines designed to minimize a smooth function subject to constraints, which may include simple bounds on the ...

    CUDA11.0-C-Programming-Guide.pdf

    17. **Breakpoint Function** - A breakpoint function has been added, allowing developers to set breakpoints for debugging purposes. This function helps in identifying and resolving issues during ...

    Efficiency balance of a light guide plate with microstructures for a see-through head-mounted display

    A simple mathematical calculation is used to determine the relationship between the real image (see-through function) energy and the virtual image energy. The simulation is based on factors taken ...

    Administrator Guide for Avaya Communication Manager

    ### Avaya Communication Manager: Administrator Guide Key Knowledge Points #### Overview of Avaya Communication Manager Avaya Communication Manager serves as the core component of Avaya's ...

    11g_plsql_user_guide_and_reference.pdf

    It eliminates the need for additional procedures or functions to fetch sequence values, streamlining the code and improving its efficiency. #### Dynamic SQL Enhancements Dynamic SQL has been ...

    sanyo denki DC SERVO SYSTEMS T

    This comprehensive guide delves into the key features, functions, and specifications of the system, providing insights into its capabilities and how it can enhance overall performance. #### ...

    C Programming

    - **#include &lt;stdio.h&gt;**: Explanation of how the standard input/output library is included to enable input and output operations. - **void main()**: Description of the main function, which is the ...

    intel integrated performance promitives for intel architecture

    Developers can access this documentation online at &lt;http://developer.intel.com&gt; to learn more about the available functions and how to integrate them into their applications. ### Using Intel IPP To...

    英文原版-The Intelligent Enterprise in the Era of Big Data 1st Edition

     … the enterprise of today has changed … wherever you sit in this new corporation … Srinivasan gives us a practical and provocative guide for rethinking our business process … calling us all to ...

    UE(官方下载)

    This guide can dramatically improve your speed and efficiency for Find/Replace Tagged expressions "Tagging" the find data allows UltraEdit/UEStudio to re-use the data similar to variable during a ...

    SAP BW Tables

    In this comprehensive guide, we will delve into the critical SAP BW tables mentioned in the provided document. SAP BW (Business Warehouse) is an enterprise-level data warehousing solution designed by ...

    Problem Solving with C++ (7th edition)

    - **Writing Your First C++ Program**: A step-by-step guide to writing a simple "Hello, World!" program, including the syntax of basic statements and functions. - **Compiling and Running the Program**:...

    DFT Compiler

    Additionally, the AutoFix function enables automatic repair of Test DRC violations at the gate level, further enhancing the efficiency of the design process. ##### Rapid Scan Synthesis Technology ...

    sympy-docs-pdf-1.0.pdf

    Understanding potential issues and pitfalls is important for avoiding errors and improving efficiency. ##### 3.1 Introduction This section introduces common gotchas and pitfalls that users might ...

Global site tag (gtag.js) - Google Analytics