元组,列表,--操作符性能
来源:互联网 发布:淘宝指数排行榜 编辑:程序博客网 时间:2024/05/19 06:35
元组,列表,--操作符性能
The time for calculating the length of a list is proportional to the length of the list, as opposed totuple_size/1, byte_size/1, andbit_size/1, which all execute in constant time.
Normally you don't have to worry about the speed of length/1, because it is efficiently implemented in C. In time critical-code, though, you might want to avoid it if the input list could potentially be very long.
Some uses of length/1 can be replaced by matching. For instance, this code
foo(L) when length(L) >= 3 -> ...
can be rewritten to
foo([_,_,_|_]=L) -> ...
(One slight difference is that length(L) will fail if theL is an improper list, while the pattern in the second code fragment will accept an improper list.)
setelement/3 copies the tuple it modifies. Therefore, updating a tuple in a loop usingsetelement/3 will create a new copy of the tuple every time.
There is one exception to the rule that the tuple is copied. If the compiler clearly can see that destructively updating the tuple would give exactly the same result as if the tuple was copied, the call tosetelement/3 will be replaced with a special destructive setelement instruction. In the following code sequence
multiple_setelement(T0) -> T1 = setelement(9, T0, bar), T2 = setelement(7, T1, foobar), setelement(5, T2, new_value).
the first setelement/3 call will copy the tuple and modify the ninth element. The two followingsetelement/3 calls will modify the tuple in place.
For the optimization to be applied, all of the followings conditions must be true:
- The indices must be integer literals, not variables or expressions.
- The indices must be given in descending order.
- There must be no calls to other function in between the calls to setelement/3.
- The tuple returned from one setelement/3 call must only be used in the subsequent call tosetelement/3.
If it is not possible to structure the code as in the multiple_setelement/1 example, the best way to modify multiple elements in a large tuple is to convert the tuple to a list, modify the list, and convert the list back to a tuple.
Note that the '--' operator has a complexity proportional to the product of the length of its operands, meaning that it will be very slow if both of its operands are long lists:
DO NOT
HugeList1 -- HugeList2
Instead use the ordsets module:
DO
HugeSet1 = ordsets:from_list(HugeList1), HugeSet2 = ordsets:from_list(HugeList2), ordsets:subtract(HugeSet1, HugeSet2)
Obviously, that code will not work if the original order of the list is important. If the order of the list must be preserved, do like this:
DO
Set = gb_sets:from_list(HugeList2), [E || E <- HugeList1, not gb_sets:is_element(E, Set)]
Subtle note 1: This code behaves differently from '--' if the lists contain duplicate elements. (One occurrence of an element in HugeList2 will removeall occurrences in HugeList1.)
Subtle note 2: This code compares lists elements using the '==' operator, while '--' uses the '=:='. If that difference is important,sets can be used instead of gb_sets, but note thatsets:from_list/1 is much slower than gb_sets:from_list/1 for long lists.
Using the '--' operator to delete an element from a list is not a performance problem:
OK
HugeList1 -- [Element]
- 元组,列表,--操作符性能
- 十六:列表、元组操作
- python列表、元组、字典、字符串操作
- 元组和列表的操作
- python 字典、列表、元组操作
- python列表元组的操作
- 列表、元组及通用序列操作
- python中的元组和列表操作
- 列表和元组常用操作【python】
- python复习:列表、元组、列表操作、字典
- python的基本操作(列表,元组)
- Python 元组、列表、字典、字符串的操作
- python 列表与元组的操作简介
- Python列表、字典、元组的一些基本操作
- python3的列表、元组、集合、字典操作
- Python 中数据结构以及操作列表、元组、字符串、字典
- python学习-1-列表,元组,字典,集合,文件操作
- Python定义并使用列表和元组操作实例
- 自己联系的一个小perl
- JSP/JS全选取消全选(checked)
- 开源框架Pushlet学习
- OmniGraffle 一款曾获得苹果设计大奖的绘图软件
- 编译安装libevent,memcache,以及php的memcached扩展
- 元组,列表,--操作符性能
- LoadRunner Sample WebTours 点击Flights出现空白解决方法
- GCC 中的编译器堆栈保护技术
- LoadRunner中文出现乱码的解决办法
- Javascript 分页 完美封装
- UVA 11426 - GCD - Extreme (II)
- Source Insight 批量注释 多行注释宏
- win7下LoadRunner监控windows资源图报错
- Android平台下与服务器数据库通信的方法