Skip to content

Instantly share code, notes, and snippets.

@c-cube
Last active August 10, 2022 00:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save c-cube/d89debca006b26972b7eb190eb5da869 to your computer and use it in GitHub Desktop.
Save c-cube/d89debca006b26972b7eb190eb5da869 to your computer and use it in GitHub Desktop.
small IO benchmark
(executable
(name main)
(libraries unix))
let n = try int_of_string (Sys.getenv "N") with _ -> 500_000_000
let (let@) f x = f x
let main file =
let ic = open_in_bin file in
let@() = Fun.protect ~finally:(fun () -> close_in_noerr ic) in
let count = ref 0 in
let buf = Bytes.create (64 * 1024) in
let n_zeroes = ref 0 in
while !count < n do
let to_read = min (Bytes.length buf) (n - !count) in
let read = input ic buf 0 to_read in
if read=0 then failwith "unexpected EOF";
count := !count + read;
(* do some processing *)
for i=0 to read-1 do
if Bytes.get buf i = '\x00' then incr n_zeroes;
done;
done;
Printf.printf "input contained %d zeroes\n" !n_zeroes
let () =
let file = Sys.argv.(1) in
Printf.printf "read %d bytes from %S\n%!" n file;
let time0 = Unix.gettimeofday() in
main file;
let elapsed = Unix.gettimeofday() -. time0 in
Printf.printf "took %.4fs (%.4f MiB/s)\n%!" elapsed (float n /. 1024. /. 1024. /. elapsed);
()
#!/bin/sh
exec dune exec --profile=release ./main.exe -- /dev/zero
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment