简体   繁体   English

可以对此Haskell代码进行哪些优化?

[英]What optimizations can be made to this Haskell code?

--Returns last N elements in list
lastN :: Int -> [a] -> [a]
lastN n xs = let m = length xs in drop (m-n) xs

--create contiguous array starting from index b within list a
produceContiguous :: [a] -> Int -> [[a]]
produceContiguous [] _ = [[]]  
produceContiguous arr ix = scanl (\acc x -> acc ++ [x]) [arr !! ix] inset
    where inset = lastN (length arr - (ix + 1)) arr

--Find maximum sum of all possible contiguous sub arrays, modulo [n]
--d is dummy data
let d = [1,2,3,10,6,3,1,47,10]
let maxResult = maximum $ map (\s -> maximum s) $ map (\c -> map (\ac -> (sum ac )`mod` (last n)) c ) $ map (\n -> produceContiguous d n ) [0..(length d) -1]

I'm a Haskell newb - just a few days into it .. If I'm doing something obviously wrong, whoopsies 我是Haskell的新手-刚加入几天。.如果我做错了明显的事情,糟糕

You can improve the runtime a lot by observing that map sum (produceContiguous dn) (which has runtime Ω(m^2), m the length of drop nd -- possibly O(m^3) time because you're appending to the end of acc on each iteration) can be collapsed to scanl (+) 0 (drop nd) (which has runtime O(m)). 您可以通过观察map sum (produceContiguous dn) (运行时为Ω(m ^ 2),距离为drop nd的长度m-可能为O(m ^ 3)的时间,来改善运行时间,因为您要追加每次迭代的acc结束)可以折叠为scanl (+) 0 (drop nd) (运行时为O(m))。 There are plenty of other stylistic changes I would make as well, but that's the main algorithmic one I can think of. 我还将进行很多其他样式方面的更改,但这是我能想到的主要算法。

Cleaning up all the stylistic stuff, I would probably write: 清理所有风格方面的东西,我可能会写:

import Control.Monad
import Data.List
addMod n x y = (x+y) `mod` n
maxResult n = maximum . (scanl (addMod n) 0 <=< tails)

In ghci: 在ghci中:

*Main> jaggedGoofyMax 100 [1..1000]
99
(12.85 secs, 24,038,973,096 bytes)
*Main> dmwitMax 100 [1..1000]
99
(0.42 secs, 291,977,440 bytes)

Not shown here is the version of jaggedGoofyMax that has only the optimization I mentioned in my first paragraph applied, which has slightly better runtime/memory usage stats to dmwitMax when run in ghci (but basically identical to dmwitMax when both are compiled with -O2 ). 这里没有显示的jaggedGoofyMax版本仅应用了我在第一段中提到的优化,当在ghci中运行时, dmwitMax运行时/内存使用情况统计更好(但与dmwitMax都使用-O2编译时基本相同) 。 So you can see that for even modest input sizes this optimization can make a big difference. 因此,您可以看到,即使输入大小适中,此优化也会有很大的不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM