- 浏览: 39116 次
- 来自: 深圳
文章分类
最新评论
-
ZacMa:
哈哈,突然感觉里面没怎么介绍,全是贴代码了
<8>redis及erl-redis阅读 -
惊涛翻案:
马博士,给我开课吧
<8>redis及erl-redis阅读
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中所有的循环都来自递归,掌握递归至关重要,为了内存考虑,尽量用尾递归而不是一般的递归
模式匹配,在函数头,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中所有的循环都来自递归,掌握递归至关重要,为了内存考虑,尽量用尾递归而不是一般的递归
发表评论
-
erlang版本安装相关问题 <32>
2014-05-10 15:54 627<1> erlang R1603安装后,crytp ... -
关于iolist<30>
2014-01-15 10:42 631iolist是比较常用的数据结构. iolist的 ... -
erlang 字符编码 <29>
2014-01-14 16:31 1264用mochiweb通过网页发送中文到服务器,结果服务器显示乱码 ... -
<27>erlang record
2013-11-19 11:19 779平时总是忘记record的某些使用方法,每次使用都要翻文档, ... -
<26>io:format io_lib:format
2013-11-14 11:07 1319使用io_lib时候要注意参数,尤其是封装json串的时候,否 ... -
<24>用error_logger间隔记录日志
2013-10-22 16:09 655执行下面的代码 test:start(). test.erl ... -
<23>erlang 数据存储
2013-10-15 22:15 1664做为后端开发者,经常 ... -
<22> erlang中的数学计算函数相关
2013-10-10 10:34 16361. 幂函数 match:pow(m,n) 表示m的n次幂 ... -
<20>erlang中的类型和函数说明
2013-09-15 11:25 986erlang是一种动态类型的语言(运行时才决定数据类型),可以 ... -
<19>erlang中的时间,日期
2013-09-06 11:21 1199时间函数涉及的数据类型: DATA TYPES datetim ... -
<18>Efficient guide 之List handling
2013-08-31 18:45 6821 Deep and flat lists lists:fl ... -
<16>Efficiency Guide之Common Caveats
2013-08-11 11:07 814(1) ++ 如果做一个list的反转,不要这样, naiv ... -
<15> lists模块补充
2013-08-05 20:12 831%% 对list模块经常用到的进行补充 %% 1 对所有元素进 ... -
<15> lists模块解析和补充
2013-07-24 17:57 12%% 对list模块经常用到的进行补充 %% 1 对所有元素 ... -
<12>简述erlang的几种错误
2013-04-14 23:31 11851) badarg Bad argument. The ar ... -
<11>erlang中方便使用的模块和命令(2)
2013-04-06 22:33 803(1) 进程字典到底用不用,很多人推荐使用 http:// ... -
<9>rabbitmq网络层
2013-01-31 00:20 820抽离出了网络层, 逻辑层待以后研究 https://gith ... -
<8>redis及erl-redis阅读
2013-01-16 10:14 8551 redis的功能相当的强大,里面的发布订阅pub/su ... -
<7>pg2 分析
2012-12-08 13:42 1260网上看到erlang的pg2模块似乎没人推荐使用,但是还是有不 ... -
<6>error_logger 使用
2012-12-02 16:24 1455erlang中日志管理主要有error_loggger 模块, ...
相关推荐
modify and extract zip archives(92KB)<END><br>48,zip2.zip<br>The library to create, modify and extract zip archives (31KB)<END><br>49,pointers.zip<br>A Beginner's Guide to Pointers<br>An article ...
The `$("<element>")` function allows developers to generate new elements dynamically. - **Example:** `var newDiv = $("<div>").text("Hello World!");` - **Extending JQuery:** One of the most ...
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 ...
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 ...
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 ...
### Avaya Communication Manager: Administrator Guide Key Knowledge Points #### Overview of Avaya Communication Manager Avaya Communication Manager serves as the core component of Avaya's ...
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 ...
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. #### ...
- **#include <stdio.h>**: 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 ...
Developers can access this documentation online at <http://developer.intel.com> to learn more about the available functions and how to integrate them into their applications. ### Using Intel IPP To...
… 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 ...
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 ...
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 ...
- **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**:...
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 ...
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 ...