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

<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
分享到:
评论

相关推荐

    CImg Reference

    - `CImgList&lt;T&gt;` is a container class for storing a list of `CImg&lt;T&gt;` objects. - It provides convenient methods for adding, removing, and accessing images in the list. **Image List Construction/...

    Effective C#

    - **Custom Equality:** Implementing `IEquatable&lt;T&gt;` to define custom equality logic. **Item 7: Understand the Pitfalls of GetHashCode()** - **Issues:** Incorrect implementation can lead to poor ...

    SCJP6 Sun Certificated Programmer for Java 6 Study Guide (Exam 310-065) 英文原版

    ### 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

    ### 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 ...

    INTRODUCTION TO PYTHON SCRIPTING FOR MAYA ARTISTS.

    =, &lt;, &gt;, &lt;=, &gt;=) 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 ...

    Lerner -- Python Workout. 50 Essential Exercises -- 2020.pdf

    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.Fortran.2003.Handbook

    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 ...

    Python Text Processing with NLTK 2.0 Cookbook.pdf

    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-...

    Chucks_VBA_Reference_rev17.docx

    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...

    a project model for the FreeBSD Project.7z

    Ingvil Hovig &lt;ingvil.hovig@skatteetaten.no&gt; Jesper Holck&lt;jeh.inf@cbs.dk&gt; John Baldwin &lt;jhb@freebsd.org&gt; John Polstra &lt;jdp@freebsd.org&gt; Kirk McKusick &lt;mckusick@freebsd.org&gt; Mark Linimon &lt;linimon@...

    C & C++ Win32 API Programming

    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 ...

    Linux Kernel 2.4 Internals

    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 ...

Global site tag (gtag.js) - Google Analytics