++运算符比|更昂贵操作员在Erlang? [英] Is ++ operator more expensive than | operator in Erlang?

查看:103
本文介绍了++运算符比|更昂贵操作员在Erlang?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读了解一些Erlang ,而我在递归一章。

  tail_sublist(_,0,SubList) - >子表; 
tail_sublist([],_,SubList) - >子表;
tail_sublist([H | T],N,SubList)当N> 0 - >
tail_sublist(T,N-1,[H | SubList])。

作者继续解释说,有一个致命的缺陷我们的代码正是因为这样产生的子列表将是相反的,我们将不得不重新反转以获得正确的输出。相反,我所做的是使用 ++ 运算符以避免稍后反转列表。

  sublist_tail([],_,Acc) - >加速; 
sublist_tail(_,0,Acc) - >加速;
sublist_tail([H | T],N,Acc) - > sublist_tail(T,N-1,加++ [H])。

我的问题是, ++ 运算符比 | 运算符更昂贵如果是,我的解决方案(使用 ++ operator)与作者的解决方案相比仍然很慢(包括反转列表以获得正确的输出)?

解决方案


是++操作符比...更贵运营商?


这取决于。如果你使用它正确,那么不。 ++ 只有当你有一个大的左手边操作数时才是危险的。



每次 code> ++ - 在左侧列表中调用操作符(如: List1 ++ List2 ),您正在创建一个新的列表,这是您的左手操作数( List1 )的副本。每个复制操作都有一个运行时,这取决于你的 List1 的长度(随着你的迭代而不断增长)。



所以,如果你先把你的值'头先',你不必在每个步骤中的整个列表中执行复制操作。这也意味着,在列表头部的 ++ 的累积也不会太糟糕,因为只有H值在每次迭代中复制一次:

  sublist_tail([H | T],N,Acc) - > sublist_tail(T,N-1,[H] ++ ACC)。 

但是,如果您已经积累头脑(因此不得不后退),您可以使用cons-operator( |

  sublist_tail([ H | T],N,Acc)→> sublist_tail(T,N-1,[H |度Acc])。 

这是正确的方式,因为(如果我错了,请纠正我) ++ 只是语法糖,并在内部实现了一个cons-operator( | )。 / p>

I was reading Learn You Some Erlang and I came upon this example in the Recursion chapter.

tail_sublist(_, 0, SubList) -> SubList;
tail_sublist([], _, SubList) -> SubList;
tail_sublist([H|T], N, SubList) when N > 0 -> 
tail_sublist(T, N-1, [H|SubList]).

As the author goes on to explain that there is a fatal flaw in our code. It being that the sub lists hence produced would be reverse and we would have to re-reverse them to get the correct output. In contrast, what I did was use the ++ operator to avoid reversing the list later.

sublist_tail([],_,Acc) -> Acc;
sublist_tail(_,0,Acc) -> Acc;
sublist_tail([H|T],N,Acc) -> sublist_tail(T,N-1,Acc++[H]).

My question is, is the ++ operator more expensive than the | operator? And if it is, would my solution (using ++ operator) still be slow compared to the author's solution (including reversing the list to get the correct output)?

解决方案

is the ++ operator more expensive than the | operator?

That depends. If you use it correctly, then no. ++ is only dangerous when you have a big left-hand-side operand.

Each time a "++"-operator is invoked on a left-hand List (like: List1 ++ List2), you are creating a new List, that is a copy of your left-hand operand (List1). Each copy operation then has a runtime, that is dependent on the length of your List1 (which keeps growing with your iterations).

So, if you prepend your values 'head first', you don't have to perform a copy-operation over the whole list in each step. This also means, accumulation with ++ at the head of the List wouldn't be so bad either, since only the "H"-value is copied once in each iteration:

sublist_tail([H|T],N,Acc) -> sublist_tail(T,N-1,[H]++Acc).

But if you are already accumulating head-first (and thus have to reverse later anyhow), you can do it with the cons-operator (|)

sublist_tail([H|T],N,Acc) -> sublist_tail(T,N-1,[H|Acc]).

This is the 'proper' way, since (please correct me if I am wrong) ++ is only syntactic sugar and is implemented internally with a cons-operator (|).

这篇关于++运算符比|更昂贵操作员在Erlang?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆