From: dolfi@zkm.de
To: caml-list@inria.fr
Subject: [Caml-list] novice puzzled by speed tests
Date: Sat, 3 Jan 2004 13:57:24 +0100 (CET) [thread overview]
Message-ID: <32894.80.131.128.216.1073134644.squirrel@bild.zkm.de> (raw)
Hi everybody,
I'm new to the list, and deeply impressed by the stability and speed of
OCaml, as well as its functional nature. My best congratulations to the
authors!
Toying around with 3.07, I found that ocamlopt.opt -unsafe (on Mandrake
9.1, Pentium 4, 2.4 GHz) actually produces slower code than ocamlopt.opt.
The program in question is a prime generator:
let count = 400000
let ptab =
let ptab = Array.create count 0 in
( ptab.(0)<- 2; ptab.(1) <- 3; ptab.(2) <- 5; ptab );;
let rec loop nr toggle psize =
( let max = truncate (sqrt (float nr)) in
let rec floop i =
if ptab.(i)>max then
( ptab.(psize) <- nr;
loop (nr+toggle) (6-toggle) (succ psize) )
else if nr mod ptab.(i) = 0 then
loop (nr+toggle) (6-toggle) psize
else
floop (succ i)
in if psize<count then floop 2 else ()
) in loop 5 2 3;
Printf.printf "prime %d: %d\n" count ptab.(pred count);;
On my box, the corresponding C program (gcc -O3) is slightly slower than
the ocamlopt.opt compiled O'Caml program, but about 25-30% faster than the
-unsafe one:
#include <stdio.h>
#include <math.h>
#define how_many 400000
int main()
{ unsigned int nr = 5, toggle = 2, max, primes_size = 3, i;
unsigned int primes[how_many];
primes[0] = 2; primes[1] = 3; primes[2] = 5;
loop:
nr += toggle; toggle = 6 - toggle;
max = sqrt(nr);
for (i = 2; primes[i] <= max; ++i)
if (!(nr % primes[i])) goto loop;
primes[primes_size++] = nr;
if (primes_size < how_many) goto loop;
printf("%i\n", primes[how_many - 1]);
return 0;
}
Of course it's good that range checking increases the speed of programs,
but, being a long-time C user, I'm a little bit puzzled by miracles like
this. I suspected that the sense of the -unsafe flag was inverted, but it
isn't: the -unsafe program dies with SEGV when I deliberately introduce a
range overflow, the safe one gets an exception.
Till soon, Dolfi
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
next reply other threads:[~2004-01-03 12:58 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-01-03 12:57 dolfi [this message]
2004-01-04 16:49 ` Xavier Leroy
2004-01-04 20:49 ` Brian Hurt
2004-01-05 19:50 ` [Caml-list] camlp4 Ker Lutyn
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=32894.80.131.128.216.1073134644.squirrel@bild.zkm.de \
--to=dolfi@zkm.de \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox