caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: effbiae@ivorykite.com
To: caml-list@inria.fr
Subject: [Caml-list] c is 4 times faster than ocaml?
Date: Wed, 4 Aug 2004 12:39:07 +1000 (EST)	[thread overview]
Message-ID: <36002.60.246.254.84.1091587147.squirrel@www.ivorykite.com> (raw)

hello,

my first post to the list.  not intended to be inflammatory or to generate
ill feeling in any way. :)

i am evaluating languages for implementing a fast dbms.  i would like to
use a 'higher level' language without resorting to portable assembler. 
ocaml looks really nice, and it drew my attention in doug's language
shootout:
 http://www.bagley.org/~doug/shootout/craps.shtml
and i have noticed that it is used to win programming contests -- indeed
the language for a discriminating hacker!

it was with great hope that i started on my first benchmark -- testing
what all fast dbmses use: mmap.  after a bit of searching, i found that
Bigarray was the way to go (short of writing my own C extension).  the
benchmark sources in c and ocaml are appended, along with the Makefile.

in summary, on my  Mandrake 10  PIII 500 system, i get these timings:
 $ time -p ./cbs 26         (* the C version *)
 real 1.06
 user 0.54
 sys 0.51
 $ time -p ./ocbs 26        (* the O'Caml version *)
 real 2.95
 user 2.39
 sys 0.51
the real time can vary a bit due to different states of cache, but user
and sys remain fairly constant.  the real time is not significant for my
purposes because the dbms will not be IO bound for most of it's queries.

so there you have it!  i would really like to be able to optimise the
ocaml benchmark to be within 10% of C.  i have read a post by John Prevost
"mmap for O'Caml" in which he implies he wrote mmap primitives but not
using the O'Caml-C interface.  what does he mean?  i assume Bigarray is
written in the fastest possible way -- or is there a faster way?

also note that i'll need msync, so i will need to extend O'Caml in some
way regardless (unless there's some library out there for mmap that i
haven't discovered).

any help greatly appreciated,


jack.
$ cat Makefile
oc:
        ocamlopt -unsafe -inline 2 bigarray.cmxa unix.cmxa -o ocbs bs.ml
c:
        gcc -O3 -o cbs bs.c

$ cat bs.ml
let f x y z = x + y + z;;
let g x = function y -> function z -> f x y z;;
let h x = let k=1 in function y ->  f x y k;;
let mapit =  let k=(-1) in function ty -> function fd ->
 Bigarray.Array1.map_file fd ty Bigarray.c_layout true k;;
let maprwbs=mapit Bigarray.int8_unsigned;;

if Array.length Sys.argv = 2 then begin
 let p=int_of_string Sys.argv.(1)
 and fn=Sys.argv.(0) ^ ".bs" in
  let fd=Unix.openfile fn [Unix.O_RDWR;Unix.O_CREAT;Unix.O_TRUNC] 0o640
  and n=1 lsl p in
   let _=Unix.lseek fd (n-1) Unix.SEEK_SET
   and _=Unix.write fd "\000" 0 1
   and _=assert (Unix.lseek fd 0 Unix.SEEK_END == n)
   and ar=mapit Bigarray.int8_unsigned fd in
    let _=for i=0 to n-1 do ar.{i} <- i done
    and odds=ref 0 in for i=0 to n-1 do
     if ar.{i} land 1 = 1 then odds:=!odds+1 done
end else begin
 print_endline "Usage: bs <power-of-2>" end;;

$ cat bs.c
#include <stdlib.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define CHKact(x,act) do \
 if(!(x)){fprintf(stderr,"!CHK (%s:%d)\n",__FILE__,__LINE__);act;} while(0)
#define CHK(x) CHKact(x,return -1)
#define CHKp(x) CHKact(x,perror(0);return -1)
main(int argc,char**argv)
{if(argc==2)
 {char fn[1024];CHK(sprintf(fn,"%s.bs",argv[0]));int p=atoi(argv[1]);
  int fd;CHKp(-1!=(fd=open(fn,O_RDWR|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR)));
  int n=1<<p;lseek(fd,n-1,SEEK_SET);int zero=0;CHKp(write(fd,&zero,1)==1);
  CHKp(lseek(fd,0,SEEK_END)==n);unsigned char*ar;
  CHKp(-1!=(int)(ar=mmap(0,n,PROT_READ|PROT_WRITE,MAP_FILE|MAP_SHARED,fd,0)));
  int i;for(i=0;i<n;++i)ar[i]=i;
  int odds=0;for(i=0;i<n;++i)if(ar[i]&1)odds++;
  CHKp(!munmap(ar,n));
 }else fprintf(stderr, "Usage: %s <power-of-2>\n",argv[0]);
}




-------------------
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


             reply	other threads:[~2004-08-04  2:39 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-08-04  2:39 effbiae [this message]
2004-08-04  4:59 ` John Prevost
2004-08-04  5:05   ` John Prevost
2004-08-04  5:24   ` effbiae
2004-08-04  7:28     ` John Prevost
2004-08-04  8:18       ` [Caml-list] " Jack Andrews
2004-08-04 10:06         ` Mikhail Fedotov
2004-08-04 10:25           ` [Caml-list] " Jack Andrews
2004-08-04 15:38             ` [Caml-list] custom mmap modeled on bigarray Jack Andrews
2004-08-10  5:06               ` Jack Andrews
2004-08-11 14:52                 ` Eric Stokes

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=36002.60.246.254.84.1091587147.squirrel@www.ivorykite.com \
    --to=effbiae@ivorykite.com \
    --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;
as well as URLs for NNTP newsgroup(s).