[英]Preallocating Data.Vector for O(1) in GHC?
In C++ world there is an optimisation for std::vector
when push_back
'ing known amount of items to new vector - preallocate space 在C ++世界中,当
push_back
已知数量的项目导入新向量时,对std::vector
了优化- 预分配空间
Is there analogue for Haskell unboxed vectors? Haskell未装箱的载体有类似物吗? Doc says
cons
is O(n)
. Doc说
cons
是O(n)
。
Data.Vector.(++)
is O(m+n) according to the docs, which is more efficient than cons
-ing n times. 根据文档,
Data.Vector.(++)
为O(m + n),效率比cons
n倍。 So if you just put the new elements in a new vector, you can append that to the old one efficiently. 因此,如果您只是将新元素放入新矢量中,则可以将其有效地附加到旧矢量上。
Note that in C++ you might do this in only O(n) provided you have enough preallocated area. 请注意,在C ++中,只要您有足够的预分配区域,就可以仅在O(n)中执行此操作。 The extra O(m) you get in Haskell, I guess, is due to Data.Vector being immutable, and requiring a copy.
我猜您在Haskell中获得的额外O(m)是由于Data.Vector是不可变的,并且需要一个副本。
If you want to use mutable vectors instead, have a look to Data.Vector.Mutable.grow
. 如果要使用可变向量,请查看
Data.Vector.Mutable.grow
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.