【问题标题】:OCaml Optimization TechniquesOCaml 优化技术
【发布时间】:2014-06-28 21:50:26
【问题描述】:

我是 OCaml 的新手(对 Haskell 有一些先验知识)。我想说服自己采用 OCaml。因此,我尝试比较 C 和 OCaml 之间的性能。我写了以下天真的 Monte Carlo Pi-finder:

C版

#include <stdio.h>
#include <stdlib.h>

int main(int argc, const char * argv[]) {

    const int N = 10000000;
    const int M = 10000000;

    int count = 0;
    for (int i = 0; i < N; i++) {
        double x = (double)(random() % (2 * M + 1) - M) / (double)(M);
        double y = (double)(random() % (2 * M + 1) - M) / (double)(M);
        if (x * x + y * y <= 1) {
            count++;
        }
    }

    double pi_approx = 4.0 * (double)(count) / (double)(N);
    printf("pi .= %f", pi_approx);
    return 0;
}

Ocaml 版本

let findPi m n = 
    let rec countPi count = function 
        | 0 -> count
        | n ->
            let x = float_of_int (Random.int (2 * m + 1) - m) /. (float_of_int m) in
            let y = float_of_int (Random.int (2 * m + 1) - m) /. (float_of_int m) in
            if x *. x +. y *. y <= 1. then
                countPi (count + 1) (n - 1)
            else
                countPi count (n - 1) in
    4.0 *. (float_of_int (countPi 0 n)) /. (float_of_int n);;

let n = 10000000 in
let m = 10000000 in

let pi_approx = findPi m n in
Printf.printf "pi .= %f" pi_approx

我用 Clang(Apple LLVM 5.1 版)编译了 C,用 ocamlopt v4.01.0 编译了 OCaml。

C的运行时间为0.105s。 OCaml 是 0.945s,慢了 9 倍。我的目标是把OCaml的运行时间减少3倍,让程序能在0.315s内完成。

由于我对 OCaml 还很陌生,所以我想学习一些 OCaml 优化技术。请给我一些建议! (已经应用了尾递归,否则程序会因stackoverflow而崩溃)

【问题讨论】:

  • 一个评论是,这很可能主要是对两个随机数生成器进行计时。它们的速度在语言和实现之间存在巨大差异。您可以尝试对两个测试使用相同的生成器。

标签: performance optimization functional-programming ocaml


【解决方案1】:

如果我在两个测试中使用相同的随机数生成器,这就是我看到的结果。

这是从 OCaml 调用 random() 的存根:

#include <stdlib.h>

#include <caml/mlvalues.h>

value crandom(value v)
{
    return Val_int(random());
}

这是修改后的 OCaml 代码:

external crandom : unit -> int = "crandom"

let findPi m n =
    let rec countPi count = function
        | 0 -> count
        | n ->
            let x = float_of_int (crandom () mod (2 * m + 1) - m) /. (float_of_int m) in
            let y = float_of_int (crandom () mod (2 * m + 1) - m) /. (float_of_int m) in
            if x *. x +. y *. y <= 1. then
                countPi (count + 1) (n - 1)
            else
                countPi count (n - 1) in
    4.0 *. (float_of_int (countPi 0 n)) /. (float_of_int n);;

let n = 10000000 in
let m = 10000000 in

let pi_approx = findPi m n in
Printf.printf "pi .= %f" pi_approx

我还原封不动地复制了你的 C 代码。

这是一个会话,展示了我的 Mac(2.3 GHz Intel Core i7)上的两个程序:

$ time findpic
pi .= 3.140129
real    0m0.346s
user    0m0.343s
sys     0m0.002s
$ time findpic
pi .= 3.140129
real    0m0.342s
user    0m0.340s
sys     0m0.001s
$ time findpiml
pi .= 3.140129
real    0m0.396s
user    0m0.394s
sys     0m0.002s
$ time findpiml
pi .= 3.140129
real    0m0.395s
user    0m0.393s
sys     0m0.002s

看起来 OCaml 代码慢了大约 15%。

我根本没有尝试让它更快,我只是用 C 代码正在使用的随机数生成器替换了随机数生成器。

您的代码实际上似乎很难改进(即,它是好的代码)。

编辑

(我重写了存根以使其更快。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-08
    • 1970-01-01
    • 2013-09-13
    • 2017-06-25
    • 2017-02-18
    • 2011-09-24
    • 2010-09-20
    相关资源
    最近更新 更多