开发者

lists:append/2 versus erlang:'++' /2, appending to end of list Versus appending to its Head

Of the two given pairs of comparisons, Which one (of each pair) is more expensive to System Resources in Erlang:

Qn1: lists:append(L1,L2) versus erlang:'++'(L1,L2)

Qn2 Writing to the head of a list with say: [NewHead|List] versus writing to the end of the list with: List ++ [NewValue]开发者_JAVA百科

I have asked this because there is an intensive part of my program which will be reading and writing into lists. I need to decide wether i will be writing to the lists' heads or writing to their ends, or vice versa.


1: They are the same function. 'append' is an alias for '++' (or vice versa). See also Erlang ++ operator. Syntactic sugar, or separate operation?

2: Don't build a list incrementally by appending. Appending once is OK, but appending in a loop will give you quadratic behaviour. I.e., AddedStuff ++ Accumulator is OK (even in a loop), because you're growing "to the left", but Accumulator ++ AddedStuff in a loop (growing to the right) is really bad. It's much better to grow to the left and then reverse or sort afterwards if the order is important.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜