Article View: fa.caml
Article #94How to avoid boxing floats in recursion?
From: Stjepan Glavina
Date: Thu, 06 Dec 2012 12:18
Date: Thu, 06 Dec 2012 12:18
35 lines
2480 bytes
2480 bytes
I am quite new to OCaml and interested in the simple mandelbrot computation presented here:
http://metamatix.org/~ocaml/price-of-abstraction.html
What I would like to optimize is this function:
let iters1 max_iter xc yc =
let rec aux count x y =
if count = max_iter then max_iter else begin
if x *. x +. y *. y >= 4.0 then count else
aux (count+1) (x *. x -. y *. y +. xc) (2.0 *. x *. y +. yc)
end in
aux 0 xc yc
The main problem with this is that arguments x and y in function aux are always boxed so that contributes to a noticeable slowdown.
I tried several approaches to make this faster, one of them is this:
type complex = {
x : float;
y : float;
}
let iters1 max_iter xc yc =
let rec aux count c =
if count = max_iter then max_iter else begin
if c.x *. c.x +. c.y *. c.y >= 4.0 then count else begin
aux (count+1) { x = c.x *. c.x -. c.y *. c.y +. xc;
y = 2.0 *. c.x *. c.y +. yc }
end
end in
aux 0 { x=xc; y=yc }
This time fields x and y are unboxed, but the record itself is boxed so I gained no speed up. The running time is the same as before.
Using references didn't help either.
I would like to understand the workings of ocamlopt better so I'm curious whether it is possible to avoid boxing in recursive functions like "aux"?
Thanks!
Technical Details
Message-ID:
<d654ce4a-e285-458a-9ea8-1078d010d851@googlegroups.com>Path:
arc1-ca.pugleaf.net!gf5ni47564750qab.0!nntp.google.com!c8no1642608qao.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: c0fe4b917e,7c3f30f5163abcd6,start
X-Google-Attributes: gidc0fe4b917e,public,usenet
X-Google-NewGroupId: yes
X-Google-Language: ENGLISH,ASCII-7-bit
Received: by 10.224.173.202 with SMTP id q10mr1395614qaz.3.1354796335653;
Thu, 06 Dec 2012 04:18:55 -0800 (PST)
Received: by 10.49.87.9 with SMTP id t9mr195277qez.16.1354796335619; Thu, 06
Dec 2012 04:18:55 -0800 (PST)
Path: arc1-ca.pugleaf.net!gf5ni47564750qab.0!nntp.google.com!c8no1642608qao.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups: fa.caml
Date: Thu, 6 Dec 2012 04:18:55 -0800 (PST)
Complaints-To: groups-abuse@google.com
Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=193.198.17.121;
posting-account=yBWvOwkAAABReMpgOmW1lMMRXkDAGCCt
NNTP-Posting-Host: 193.198.17.121
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <d654ce4a-e285-458a-9ea8-1078d010d851@googlegroups.com>
Subject: How to avoid boxing floats in recursion?
From: Stjepan Glavina <stjepang@gmail.com>
Injection-Date: Thu, 06 Dec 2012 12:18:55 +0000
Content-Type: text/plain; charset=ISO-8859-1