如何优化这些 ocaml 函数以实现动态间隔调度?

发布于 2024-08-10 13:56:35 字数 883 浏览 3 评论 0原文

我有一个程序可以使用动态规划解决加权间隔调度问题 (无论你相信与否,这不是为了家庭作业)。我已经对其进行了分析,并且我似乎花费了大部分时间用 p(...) 填充 M。以下是函数:

let rec get_highest_nonconflicting prev count start =
  match prev with
      head :: tail -> 
    if head < start then 
      count
    else
      get_highest_nonconflicting tail (count - 1) start
    | [] -> 0;;

let m_array = Array.make (num_genes + 1) 0;;    

let rec fill_m_array ?(count=1) ?(prev=[]) gene_spans  = 
  match gene_spans with
      head :: tail -> m_array.(count) <- 
    get_highest_nonconflicting prev (count - 1) (get_start head);
    fill_m_array tail ~prev:(get_stop (head) :: prev) ~count:(count+1);
    | [] -> ();;

我真的想不出任何方法来优化它,并且根据我对这个算法的了解,这似乎是可能占用最多时间的地方。但这也只是我的第二个 OCaml 程序。那么有没有什么办法可以优化这个呢?

I have a program that solves the weighted interval scheduling problem using dynamic programming (and believe it or not, it isn't for homework). I've profiled it, and I seem to be spending most of my time filling M with p(...). Here are the functions:

let rec get_highest_nonconflicting prev count start =
  match prev with
      head :: tail -> 
    if head < start then 
      count
    else
      get_highest_nonconflicting tail (count - 1) start
    | [] -> 0;;

let m_array = Array.make (num_genes + 1) 0;;    

let rec fill_m_array ?(count=1) ?(prev=[]) gene_spans  = 
  match gene_spans with
      head :: tail -> m_array.(count) <- 
    get_highest_nonconflicting prev (count - 1) (get_start head);
    fill_m_array tail ~prev:(get_stop (head) :: prev) ~count:(count+1);
    | [] -> ();;

I can't really think of any ways to optimize this, and based on my knowledge of this algorithm, this seems to be the place that is likely to take up the most time. But this is also only my second OCaml program. So is there any way to optimize this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情独悲 2024-08-17 13:56:35

您的两个功能没有任何明显低效的地方。您是否期望您的实现速度更快,例如参考另一种语言的实现?

我想知道传递给 get_highest_nonconflicting 的列表。如果您有理由期望此函数经常传递与之前传递的列表在物理上相同的列表(这包括在递归调用中传递的子列表),那么那里的缓存可能会有所帮助。

如果您期望列表相等但物理上不同,则散列构造(然后是缓存)可能会有所帮助。

There isn't anything obviously inefficient with your two functions. Did you expect your implementation to be faster, for instance with reference to an implementation in another language?

I was wondering about the lists passed to get_highest_nonconflicting. If you have reasons to expect that this function is often passed lists that are physically identical to previously passed lists (and this includes the sub-lists passed on recursive calls), a cache there could help.

If you expect lists that are equal but physically different, hash-consing (and then caching) could help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文