You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Original bug ID: 397 Reporter: administrator Status: closed Resolution: not a bug Priority: normal Severity: minor Category: ~DO NOT USE (was: OCaml general)
Bug description
Hi,
I'm learning OCAML and I've downloaded your compiler (ocamlopt) for SPARC. I
was trying to compare the performance of gcc versus ocamlopt. My first test
program was a loop that adds the numbers from 1 to 1 billion. I noticed two
things that I think need to be redressed:
ocamlopt (and ocamlc) do not detect tail recursion in the following case:
let rec sum n = if n = 0 then 0 else n + sum (n - 1) in
sum 1000000000;;
This little program causes a stack overflow, needlessly. The compiler does
detect tail recursion in the more trivial case:
let rec stupid n = if n = 0 then 0 else stupid (n - 1) in
stupid 1000000000;;
When I compared the performance of the iterated version of the program:
let a = ref 0 in
for i = 1 to 1000000000 do
a := !a + i
done ;;
with a C equivalent, the C program took 40% of the time required for the
OCAML version. When I looked at the assembly code, I found out that ocamlopt
generated code that recomputes the upper bound of the loop at every
iteration. This is not only suboptimal (since the boundary is a constant)
but also seems to contradict the semantics of the 'for' command, that call
for a one-time computation of the loop boundary values, at the start of the
loop. Had the compiler not done this repeated computation, the OCAML program
would have run, at my estimation, about 30-40% faster.
Thanks,
Dan Arnon
The text was updated successfully, but these errors were encountered:
Original bug ID: 397
Reporter: administrator
Status: closed
Resolution: not a bug
Priority: normal
Severity: minor
Category: ~DO NOT USE (was: OCaml general)
Bug description
Hi,
I'm learning OCAML and I've downloaded your compiler (ocamlopt) for SPARC. I
was trying to compare the performance of gcc versus ocamlopt. My first test
program was a loop that adds the numbers from 1 to 1 billion. I noticed two
things that I think need to be redressed:
let rec sum n = if n = 0 then 0 else n + sum (n - 1) in
sum 1000000000;;
This little program causes a stack overflow, needlessly. The compiler does
detect tail recursion in the more trivial case:
let rec stupid n = if n = 0 then 0 else stupid (n - 1) in
stupid 1000000000;;
let a = ref 0 in
for i = 1 to 1000000000 do
a := !a + i
done ;;
with a C equivalent, the C program took 40% of the time required for the
OCAML version. When I looked at the assembly code, I found out that ocamlopt
generated code that recomputes the upper bound of the loop at every
iteration. This is not only suboptimal (since the boundary is a constant)
but also seems to contradict the semantics of the 'for' command, that call
for a one-time computation of the loop boundary values, at the start of the
loop. Had the compiler not done this repeated computation, the OCAML program
would have run, at my estimation, about 30-40% faster.
Thanks,
Dan Arnon
The text was updated successfully, but these errors were encountered: