Stack performance in programming languages

前端 未结 8 2227
孤城傲影
孤城傲影 2020-12-31 17:41

Just for fun, I tried to compare the stack performance of a couple of programming languages calculating the Fibonacci series using the naive recursive algorithm. The code is

8条回答
  •  佛祖请我去吃肉
    2020-12-31 18:14

    You say very little about your configuration (in benchmarking, details are everything: commandlines, computer used, ...)

    When I try to reproduce for OCaml I get:

    let rec f n = if n < 2 then 1 else (f (n-1)) + (f (n-2))
    
    let () = Format.printf "%d@." (f 40)
    
    
    $ ocamlopt fib.ml
    $ time ./a.out 
    165580141
    
    real    0m1.643s
    

    This is on an Intel Xeon 5150 (Core 2) at 2.66GHz. If I use the bytecode OCaml compiler ocamlc on the other hand, I get a time similar to your result (11s). But of course, for running a speed comparison, there is no reason to use the bytecode compiler, unless you want to benchmark the speed of compilation itself (ocamlc is amazing for speed of compilation).

提交回复
热议问题