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

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

相关推荐

    C++性能优化 指南(强列推荐) chm版

    Optimizing IO &lt;br&gt; Efficient Screen Output &lt;br&gt; Efficient Binary File IO &lt;br&gt; Efficient Text File IO &lt;br&gt; Summary &lt;br&gt; &lt;br&gt; Chapter 13. Optimizing Your Code Further &lt;br&gt; Arithmetic Operations &lt;br&gt; ...

    IMAGE and VIDEO COMPRESSION for MULTIMEDIA ENGINEERING Fundamentals, Algorithms, and Standards.part1.rar

    Compensated Predictive Coding&lt;br&gt;3.6 Information-Preserving Differential Coding&lt;br&gt;3.7 Summary &lt;br&gt;3.8 Exercises&lt;br&gt;References&lt;br&gt;Chapter 4 Transform Coding&lt;br&gt;4.1 Introduction&lt;br&gt;4.1.1 Hotelling ...

    Visual C++ 编程资源大全(英文源码 字符串)

    UniString.zip&lt;br&gt;Class to manipulate UNICODE string on Window 95/98.(6KB)&lt;END&gt;&lt;br&gt;11,faststring.zip&lt;br&gt;Fast and efficient CString replacement(63KB)&lt;END&gt;&lt;br&gt;12,StdString.zip&lt;br&gt;CString like class using...

    8086产品手册.rar

    Y Direct Addressing Capability 1 MByte&lt;br&gt;of Memory&lt;br&gt;Y Architecture Designed for Powerful&lt;br&gt;Assembly Language and Efficient High&lt;br&gt;Level Languages&lt;br&gt;Y 14 Word, by 16-Bit Register Set with&lt;br&gt;...

    Mpeg2_Decoder

    mpeg2encode / mpeg2decode&lt;br&gt; =========================&lt;br&gt; MPEG-2 Encoder / Decoder, Version 1.2, July 19, 1996&lt;br&gt;&lt;br&gt; Copyright (c) 1996 &lt;br&gt; MPEG Software Simulation Group&lt;br&gt;&lt;br&gt; E-mail: mssg@...

    Fourier Analysis on Finite Groups with Applications in Signal Processing and System Design

    &lt;br&gt;&lt;br&gt;Fourier Analysis on Finite Groups with Applications in Signal Processing and System Design &lt;br&gt;&lt;br&gt;信号处理和系统分析中有限群的傅立叶分析和应用&lt;br&gt;&lt;br&gt;by Radomir S. Stankovic , Claudio Moraga ...

    FT Report - Investing In China

    For exporters seeking&lt;br&gt;efficient links in their supply chains, China is frequently the price setter and an&lt;br&gt;unrivalled source of cost advantage.&lt;br&gt;Both of these trends stand out in the ...

    Network+ Exam Cram 2 ,Second Edition

    &lt;br&gt;Network+ Exam Cram 2 ,Second Edition &lt;br&gt;&lt;br&gt;Exam Cram N10-003&lt;br&gt;&lt;br&gt;By Drew Bird, Mike Harwood &lt;br&gt; &lt;br&gt;Publisher: Que &lt;br&gt;Pub Date: June 08, 2005 &lt;br&gt;ISBN: 0-7897-3254-8 &lt;br&gt;Pages: 528 &lt;br&gt; &lt;br...

    椭圆曲线密码学引论

    Overview The book has a strong focus on efficient methods for finite field arithmetic&lt;br&gt;(Chapter 2) and elliptic curve arithmetic (Chapter 3). Next, Chapter 4 surveys the&lt;br&gt;known attacks on the ...

    CISA Exam Cram: Certified Information Systems Auditor

    &lt;br&gt;CISA Exam Cram: Certified Information Systems Auditor (Exam Cram 2)&lt;br&gt;&lt;br&gt;by Allen Keele , Keith Mortier &lt;br&gt;&lt;br&gt;&lt;br&gt;Paperback: 456 pages &lt;br&gt;Publisher: Que (April 30, 2005) &lt;br&gt;Language: English...

    perfectdisk

    defragmentation technology provides fast, efficient and complete defragmentation of even your largest drives with minimal system resource usage.&lt;br&gt;&lt;br&gt;PerfectDisk's Command Center provides powerful ...

    Apress.Accelerated.C.Sharp.2008.Nov.2007

    You'll also &lt;br&gt; learn best practices that ensure your code will be efficient, reusable, &lt;br&gt; and robust. Why spend months or years discovering the best ways to &lt;br&gt; design and code C# when this book...

    The Business Case for Storage Networks

    2004 &lt;br&gt;ISBN : 1-58720-118-6 &lt;br&gt;Pages : 264 &lt;br&gt; &lt;br&gt; &lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;Understand the business case for storage networks and lower your total cost of ownership with this comprehensive guide&lt;br&gt;&lt;br&gt;...

    ACEEE-datacenters

    Studying the more efficient of these facilities enabled us to&lt;br&gt;compile a set of “best-practice” technologies for energy efficiency. These best practices include:&lt;br&gt;improved air management, ...

    Writing Efficient C for ARM

    &lt;br&gt;&lt;br&gt;The ARM and Thumb C compilers are mature, industrial-strength ANSI C compilers which&lt;br&gt;are capable of producing high quality machine code. However, when writing source code,&lt;br&gt;it is always ...

    空间数据库英文版

    A large number of research efforts have appeared in literature aimed at&lt;br&gt;effective modelling of spatial data and efficient processing of spatial queries.&lt;br&gt;This book investigates several aspects ...

    Developing Statistical Software in Fortran 95

    Preface v&lt;br&gt;1 Motivation 1&lt;br&gt;1.1 WhyWeWrote This Book . . . . . . . . . . . . . . . . . . . 1&lt;br&gt;1.2 ForWhomWe AreWriting . . . . . . . . . . . . . . . . . . 2&lt;br&gt;1.2.1 Those Accustomed to Fortran ...

    CodeRush with Refactor! Pro 2.5.1

    ™ Pro&lt;br&gt; &lt;br&gt; &lt;br&gt; &lt;br&gt;Powerful. Efficient. Fast.&lt;br&gt;&lt;br&gt;Today, software consumers demand more than ever before from developers. The pressure to deliver more features with higher quality in less ...

    Embedded Linux Primer

    Book Description&lt;br&gt;&lt;br&gt;Comprehensive Real-World Guidance for Every Embedded Developer and Engineer&lt;br&gt;&lt;br&gt;This book brings together indispensable knowledge for building efficient, high-value, Linux-...

Global site tag (gtag.js) - Google Analytics