<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

你可能感兴趣的:(list)