<18>Efficient guide 之List handling

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值