The documentation of Data.Array
reads:
Haskell provides indexable arrays, which may be thought of as functions whose domains are isomorphic to contiguous subsets of the integers. Functions restricted in this wa开发者_C百科y can be implemented efficiently; in particular, a programmer may reasonably expect rapid access to the components.
I wonder how fast can (!)
and (//)
be. Can I expect O(1) complexity from these, as I would have from their imperative counterparts?
In general, yes, you should be able to expect O(1) from !
although I'm not sure if thats guaranteed by the standard.
You might want to see the vector package if you want faster arrays though (through use of stream fusion). It is also better designed.
Note that //
is probably O(n) though because it has to traverse the list (just like an imperative program would). If you need a lot of mutation you can use MArray
orMVector
.
精彩评论