【发布时间】:2014-08-26 21:35:23
【问题描述】:
我需要帮助优化此代码,这需要 12 秒,我需要大约 4 秒。
let publication idx (lst: string [] list) = // returns a specific value of the string [] list
lst
|>Array.map (fun arr -> arr.[idx])
let rec Tuple (x:int) (ID:string list) (Information:string [] list) =
if x < ID.Length then
let muro = [|(ID.[x], Information|> List.filter (fun elem -> elem.[1] = humanID.[x] )|> publication 0 |> List.toArray )|]
let rest = Tuple (x+1) ID Information
Array.append muro rest
else [||]
let FinalTuple= Tuple 0 ID Information
finalTuple 是一个:(string*string []) []
递归需要很长时间才能完成,我似乎无法让它更快(ID.Length 为 1600)
感谢您的帮助
【问题讨论】:
-
为什么要将列表转换为数组并返回?这是在浪费大量时间。
-
解决了这个问题,没有什么影响
-
你还在
publication做这件事 -
再次修复了这个问题,我这样做是因为几天前有人告诉我,在数组上映射比在列表上映射更快(仍然没有区别)
-
当然,但它不更快地转换为数组、映射然后再转换回列表。
标签: list optimization recursion f# tuples