- 浏览: 39093 次
- 来自: 深圳
文章分类
最新评论
-
ZacMa:
哈哈,突然感觉里面没怎么介绍,全是贴代码了
<8>redis及erl-redis阅读 -
惊涛翻案:
马博士,给我开课吧
<8>redis及erl-redis阅读
1
Deep and flat lists
lists:flatten的代价非常高,甚至比 ++的代价更高,所以要尽可能避免使用,
可以有几种方法避免,
(1), 发送list到一个port时候,ports可以解析深度lists, 因此不用转化,传入深度list即可
(2)一些bif可以支持深度list,例如 list_to_binary/1 or iolist_to_binary/1 也是支持深度list
(3)如果list是仅仅1层,可以用lists:append代替, 原文
lists:flatten/1 builds an entirely new list. Therefore, it is expensive, and even more expensive than the ++ (which copies its left argument, but not its right argument).
In the following situations, you can easily avoid calling lists:flatten/1:
When sending data to a port. Ports understand deep lists so there is no reason to flatten the list before sending it to the port.
When calling BIFs that accept deep lists, such as list_to_binary/1 or iolist_to_binary/1.
When you know that your list is only one level deep, you can can use lists:append/1.
Port example
DO
...
port_command(Port, DeepList)
...
DO NOT
...
port_command(Port, lists:flatten(DeepList))
...
Append example
DO
> lists:append([[1], [2], [3]]).
[1,2,3]
>
DO NOT
> lists:flatten([[1], [2], [3]]).
[1,2,3]
>
2
递归和尾递归在性能上区别不是很大,不过递归要比尾递归浪费更多空间,并且空间的大小和列表的长短是成比例的
In the performance myth chapter, the following myth was exposed: Tail-recursive functions are MUCH faster than recursive functions.
To summarize, in R12B there is usually not much difference between a body-recursive list function and tail-recursive function that reverses the list at the end. Therefore, concentrate on writing beautiful code and forget about the performance of your list functions. In the time-critical parts of your code (and only there), measure before rewriting your code.
Important note: This section talks about lists functions that construct lists. A tail-recursive function that does not construct a list runs in constant space, while the corresponding body-recursive function uses stack space proportional to the length of the list. For instance, a function that sums a list of integers, should not be written like this
Deep and flat lists
lists:flatten的代价非常高,甚至比 ++的代价更高,所以要尽可能避免使用,
可以有几种方法避免,
(1), 发送list到一个port时候,ports可以解析深度lists, 因此不用转化,传入深度list即可
(2)一些bif可以支持深度list,例如 list_to_binary/1 or iolist_to_binary/1 也是支持深度list
(3)如果list是仅仅1层,可以用lists:append代替, 原文
lists:flatten/1 builds an entirely new list. Therefore, it is expensive, and even more expensive than the ++ (which copies its left argument, but not its right argument).
In the following situations, you can easily avoid calling lists:flatten/1:
When sending data to a port. Ports understand deep lists so there is no reason to flatten the list before sending it to the port.
When calling BIFs that accept deep lists, such as list_to_binary/1 or iolist_to_binary/1.
When you know that your list is only one level deep, you can can use lists:append/1.
Port example
DO
...
port_command(Port, DeepList)
...
DO NOT
...
port_command(Port, lists:flatten(DeepList))
...
Append example
DO
> lists:append([[1], [2], [3]]).
[1,2,3]
>
DO NOT
> lists:flatten([[1], [2], [3]]).
[1,2,3]
>
2
递归和尾递归在性能上区别不是很大,不过递归要比尾递归浪费更多空间,并且空间的大小和列表的长短是成比例的
In the performance myth chapter, the following myth was exposed: Tail-recursive functions are MUCH faster than recursive functions.
To summarize, in R12B there is usually not much difference between a body-recursive list function and tail-recursive function that reverses the list at the end. Therefore, concentrate on writing beautiful code and forget about the performance of your list functions. In the time-critical parts of your code (and only there), measure before rewriting your code.
Important note: This section talks about lists functions that construct lists. A tail-recursive function that does not construct a list runs in constant space, while the corresponding body-recursive function uses stack space proportional to the length of the list. For instance, a function that sums a list of integers, should not be written like this
发表评论
-
erlang版本安装相关问题 <32>
2014-05-10 15:54 627<1> erlang R1603安装后,crytp ... -
关于iolist<30>
2014-01-15 10:42 630iolist是比较常用的数据结构. iolist的 ... -
erlang 字符编码 <29>
2014-01-14 16:31 1264用mochiweb通过网页发送中文到服务器,结果服务器显示乱码 ... -
<27>erlang record
2013-11-19 11:19 778平时总是忘记record的某些使用方法,每次使用都要翻文档, ... -
<26>io:format io_lib:format
2013-11-14 11:07 1318使用io_lib时候要注意参数,尤其是封装json串的时候,否 ... -
<24>用error_logger间隔记录日志
2013-10-22 16:09 654执行下面的代码 test:start(). test.erl ... -
<23>erlang 数据存储
2013-10-15 22:15 1664做为后端开发者,经常 ... -
<22> erlang中的数学计算函数相关
2013-10-10 10:34 16351. 幂函数 match:pow(m,n) 表示m的n次幂 ... -
<20>erlang中的类型和函数说明
2013-09-15 11:25 984erlang是一种动态类型的语言(运行时才决定数据类型),可以 ... -
<19>erlang中的时间,日期
2013-09-06 11:21 1198时间函数涉及的数据类型: DATA TYPES datetim ... -
<17>Efficiency Guide之Function
2013-08-27 22:30 5811. 函数模式匹配 模式匹配,在函数头,case和receiv ... -
<16>Efficiency Guide之Common Caveats
2013-08-11 11:07 814(1) ++ 如果做一个list的反转,不要这样, naiv ... -
<15> lists模块补充
2013-08-05 20:12 830%% 对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 802(1) 进程字典到底用不用,很多人推荐使用 http:// ... -
<9>rabbitmq网络层
2013-01-31 00:20 818抽离出了网络层, 逻辑层待以后研究 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 1454erlang中日志管理主要有error_loggger 模块, ...
相关推荐
- `CImgList<T>` is a container class for storing a list of `CImg<T>` objects. - It provides convenient methods for adding, removing, and accessing images in the list. **Image List Construction/...
- **Custom Equality:** Implementing `IEquatable<T>` to define custom equality logic. **Item 7: Understand the Pitfalls of GetHashCode()** - **Issues:** Incorrect implementation can lead to poor ...
### SCJP6 Sun Certified Programmer for Java 6 Study Guide (Exam 310-065) #### 1. Declarations and Access Control In the first chapter, the focus is on understanding declarations and access control ...
### SCJP Sun® Certified Programmer for Java™ 6 Study Guide Chapter 5: Flow Control, Exceptions, and Assertions #### Certification Objectives Overview In this chapter, we delve into the critical ...
=, <, >, <=, >=) are used to compare values. **Controlling the Flow of Execution** Control structures such as conditionals (if-else statements) and loops (while and for loops) are essential for ...
The "Python Workout: 50 Essential Exercises" is a comprehensive guide designed to help beginners and intermediate Python programmers refine their skills through practical exercises. Each exercise is ...
The book includes a list of references for further reading, providing additional resources for those interested in delving deeper into the language's features and history. #### Fortran Concepts and ...
in the form of a list of words, into a list of tuples. It also explains taggers, which are trainable. Chapter 5, Extracting Chunks, explains the process of extracting short phrases from a part-of-...
A guide on how to create a button that opens the Visual Basic Editor (VBE) directly from within Excel. This can be particularly helpful for frequent VBA developers. **Show Developer Toolbar Tab (Page...
Ingvil Hovig <ingvil.hovig@skatteetaten.no> Jesper Holck<jeh.inf@cbs.dk> John Baldwin <jhb@freebsd.org> John Polstra <jdp@freebsd.org> Kirk McKusick <mckusick@freebsd.org> Mark Linimon <linimon@...
The Win32 API is a set of programming interfaces that provides direct access to the underlying operating system functionalities, enabling developers to create robust and efficient applications for ...
It involves managing processes, handling interrupts, and ensuring efficient scheduling and synchronization. 1. **Task Structure and Process Table (Section 2.1):** - Each process in the kernel is ...