给出以下(煮沸和伪Haskell-ish)代码:
import qualified Data.Vector.Unboxed as V
minimum3 :: Double -> Double -> Double -> Double
minimum3 a b c | a < b | a < c = a
| otherwise = c
| otherwise | b < c = b
| otherwise c
readMinimum3 :: V.Vector Double -> Int -> Double
readMinimum3 v i = do
a <- V.unsafeRead v (i+0)
b <- V.unsafeRead v (i+1)
c <- V.unsafeRead v (i+2)
-- manually unrolled version of:
-- [a,b,c] <- sequence [V.unsafeRead v (i+j) | j <- [0..2]]
return $! minimum3 a b c
Run Code Online (Sandbox Code Playgroud)
minimum3是一个更大的功能,median15但想法是一样的.所有这一切都是为了防止内存分配,事实上这个代码被编译成基本上线性的汇编程序.
我也尝试过同样的事情,vectors希望融合会消除保持读取值的中间向量,但没有运气.
问题是,是否有任何方法可以用更易维护的方式编写它而无需手动展开它?
| 归档时间: |
|
| 查看次数: |
155 次 |
| 最近记录: |