Reputation: 7836
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]
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.
Upvotes: 7
Views: 4494
Reputation: 10557
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.
Upvotes: 20