Advertisement
Guest User

Untitled

a guest
Feb 11th, 2011
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 3.37 KB | None | 0 0
  1. open Benchmark
  2.  
  3. (**** THIS CODE UNDER GPL LICENSE FROM CDK/extlib ****)
  4. let resize s newlen =
  5.   let len = String.length s in
  6.   if len > newlen then String.sub s 0 newlen
  7.   else
  8.   let str = String.create newlen in
  9.   String.blit s 0 str 0 len;
  10.   str
  11.  
  12. let cdk_read buf_size name =
  13.   let chan = open_in name in
  14.   let buf = String.create buf_size in
  15.   let rec iter buf nb_read =
  16.     let buf_size = String.length buf in
  17.     let tmp = input chan buf nb_read (buf_size - nb_read) in
  18.     if tmp = 0 then
  19.       String.sub buf 0 nb_read
  20.     else
  21.       let nb_read = nb_read + tmp in
  22.       let buf =
  23.     if nb_read = buf_size then
  24.           resize buf (2 * buf_size)
  25.     else buf
  26.       in
  27.       iter buf nb_read
  28.   in
  29.   let buf = iter buf 0 in
  30.   close_in chan;
  31.   buf
  32. (**** END CDK/extlib code ****)
  33.  
  34.  
  35. let varbuf_unix tmpsize fn =
  36.   let fd = Unix.openfile fn [Unix.O_RDONLY] 0o600 in
  37.   let buf = Buffer.create tmpsize in
  38.   let tmp = String.create tmpsize in
  39.   let rec loop () =
  40.     let n = Unix.read fd tmp 0 tmpsize in
  41.     if n > 0 then (
  42.       Buffer.add_substring buf tmp 0 n;
  43.       loop ()
  44.     )
  45.   in
  46.   loop ();
  47.   Buffer.contents buf
  48.  
  49. let varbuf_perv tmpsize fn =
  50.   let fd = open_in fn in
  51.   let buf = Buffer.create tmpsize in
  52.   let tmp = String.create tmpsize in
  53.   let rec loop () =
  54.     let n = input fd tmp 0 tmpsize in
  55.     if n > 0 then (
  56.       Buffer.add_substring buf tmp 0 n;
  57.       loop ()
  58.     )
  59.   in
  60.   loop ();
  61.   Buffer.contents buf
  62.  
  63. let read_file_as_str fn =
  64.   let ic = Pervasives.open_in_bin fn in
  65.   let len = (Pervasives.in_channel_length ic) in
  66.   let old_gc = Gc.get() in
  67.   Gc.set {old_gc with Gc.space_overhead = 0};
  68.   let ret = String.create len in
  69.   Gc.set old_gc;
  70.   Pervasives.really_input ic ret 0 len;
  71.   Pervasives.close_in ic;
  72.   ret
  73.  
  74. open Bigarray
  75.  
  76. type bigstring_t = {
  77.   bigarr : (char, int8_unsigned_elt, c_layout) Array1.t;
  78.   data : string; length : int
  79. }
  80.  
  81.  
  82. let map_file fd ?pos ?(shared=false) len =
  83.   let ba = Array1.map_file fd ?pos char c_layout shared len in
  84.   let s = (Obj.magic (Obj.field (Obj.repr ba) 1) : string) in
  85.     { bigarr = ba; data = s; length = Array1.dim ba }
  86.  
  87. let mmap_fn fn =
  88.   let fd = Unix.openfile fn [Unix.O_RDONLY] 0o600 in
  89.   let len = (Unix.stat fn).Unix.st_size in
  90.   map_file fd len
  91.  
  92.  
  93. (*
  94. type buffer = (char, int8_unsigned_elt, c_layout) Array1.t
  95. external pread : Unix.file_descr -> buffer -> int64 -> int = "caml_maid_pread"
  96.  
  97. let pread_file fn =
  98.   let fd = Unix.openfile fn [Unix.O_RDONLY] 0o600 in
  99.   let len = (Unix.stat fn).Unix.st_size in
  100.   let buf = Array1.create
  101.   pread fd buf
  102. *)
  103.  
  104. open Batteries
  105.  
  106. let batio_read fn = File.with_file_in fn BatIO.read_all
  107.  
  108. let tests fn =
  109.     [ "batio", batio_read |- ignore, fn;
  110.       "cdk_orig", cdk_read 1024 |- ignore, fn;
  111.       "cdk2k", cdk_read 2048 |- ignore, fn;
  112.       "cdk4k", cdk_read 4096 |- ignore, fn;
  113.       "vbu1k", varbuf_unix 1024 |- ignore, fn;
  114.       "vbu2k", varbuf_unix 2048 |- ignore, fn;
  115.       "vbu4k", varbuf_unix 4096 |- ignore, fn;
  116.       "vbp1k", varbuf_perv 1024 |- ignore, fn;
  117.       "vbp2k", varbuf_perv 2048 |- ignore, fn;
  118.       "vbp4k", varbuf_perv 4096 |- ignore, fn;
  119.       "bitstring", Bitstring.bitstring_of_file |- ignore, fn;
  120.       "str_only", read_file_as_str |- ignore, fn;
  121.       "mmap_fn", mmap_fn |- ignore, fn;
  122.     ]
  123.  
  124. let () =
  125.   let fn = Sys.argv.(1) in
  126.   latencyN ~repeat:10 10L (tests fn) |> tabulate
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement