Reputation: 7299
I need to execute the following operation:
collection.slice(x, y)
....
collection.slice(x+1, y+1)
...
collection.slice(x+n, y+n)
or
collection.slice(x-n, y-n)
Very often execution of this part of code is expected, so I want to make it as fast as possible. What collection should I choose? (can be immutable).
P.S. on Performance Characteristics page "apply" characteristic is responsible for slicing?
P.P.S looking for articles, manuals dedicated on subject.
Upvotes: 2
Views: 1380
Reputation: 297265
Well, Vector
has logN
performance, though the constant factor may be too heavy depending on your requirements.
On the other hand, maybe range
does what you want? If it does, then a SortedMap
might well provide a logN
performance with smaller constant factor. Maybe.
Upvotes: 1
Reputation: 167901
The answer depends somewhat on how far apart x and y are, but you are generally best off using plain arrays. If you don't need to do anything with the slices, using views might be faster, but access is slower with views, so it's probably better to stay away from them.
Anyway, you certainly want something with fast "apply" (log or constant time), and between Array
, Vector
, and ArrayBuffer
, Array
is the fastest, ArrayBuffer
is about 50% slower, and Vector
is about twice as slow again for slicing and using every element that you've sliced.
Also, consider whether sliding
will do what you want. It's not as fast as the direct slicing, but it's awfully convenient.
Upvotes: 1