scala - Complexity of List.reverse? -


In Scala, the reverse method for the list is the complexity of this method? Is it better to use this basic list and always remember that what we expect from the list, or explicitly use reverse before working on it.

Edit: Am I really interested to get to the last two elements of the original list (or the first two of the inverted list).

Then I will do something like this:

  val myList = origList.reverse val a = myList (0) val b = myList (1)   

This is not in a loop in my library, only once ... but if someone uses another library and puts it in a loop, then it is not under my control < / Div>

Looking at the source, it is expected to o (n) as you can reasonably:

  Override def reverse: list [A] = {var result: list [A] = blue These are = this time (! These.ISETT) {Results = this.head :: results = these.tail} result}   

If you list through your code in reverse order From the repeated order in the preorder, if it is re-enabled at the same cost of repetition, then it will be more efficient to list it instead of behind it.

In fact, if your optional operation less using less code> o (n) time, using the original option, its There is a real logic to go with. Creating an algorithm will be as fast as possible, if you ever rely heavily on it (especially if used inside other ends, Oxbow-Lex will explain below).

On the whole I hope that anything you are pushing behind a list means that you care about the relative order of a non-trivial number of elements, and whatever you are doing He is naturally O (N) anyway. (This can not be true for other data structures like binary tree, but lists are linear, and even in extreme conditions can not be done in the time of reverse.heard O (1) single-link Listed.)

So if you are selecting between two O (N) options - Large For most applications, some nanosecond shaving is really going You are going to achieve anything so that To be readable will be "best" - which means calling reverse and then again, if it is closest to your intentions.

(And if your app is very slow, and profiling shows that this list manipulation is a hotspot, then You can think of making it more efficient. At that point your existing candidates may have a different option, additional references will be given at that time.)

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -