List Info

Thread: "ocaml_beginners"::[] Fast IO




"ocaml_beginners"::[] Fast IO
user name
2006-12-19 07:26:46

Once again I've hit a barrier and can't make mine work any faster.
Using Pervasives.input to get 4096 bytes at a time is actually slower
than using input_byte, perhaps because input_byte lets you skip using
int_of_char. I'd appreciate any tips on how to speed up either of
these implementations.

This code uses input_byte and scored 3.69 seconds.

let buff = [|0;0;0;0;0;0;0;0;0;0|];;

let get_line () =
let rec aux i =
try
let char = input_byte stdin in
if char = 10 || char = 32 then
i
else
(
buff.(i) <- char-48;
aux (i+1)
)
with End_of_file -> i in
aux 0;;

let get_int () =
let linelen = get_line () in
let ib = ref 0 in
for i=0 to linelen-1 do
ib := !ib*10+;buff.(i)
done;
!ib;;

let n = get_int ();;
let k = get_int ();;

let rec count counter = function
0 -> counter
| x -> if (get_int ()) mod k = 0 then count (counter&#43;1) (x-1) else
count counter (x-1);;

print_int (count 0 n);;

exit 0;;

This code uses input and scored 4.38 seconds.

let blocksize = 4096;;

type buffer = {buffer : string; mutable position : int}

let nth b pos =
if pos >= b.position then
invalid_arg "nth&quot;
else b.buffer.[pos]

let strbuff = {buffer = String.create blocksize; position = 0};;

let fillbuffer () =
let innum = input stdin strbuff.buffer 0 blocksize in
if innum = 0 then raise End_of_file else
strbuff.position <- innum;;

let readpos = ref 0;;

let rec get_char () =
try
let c = int_of_char (nth strbuff !readpos) in
readpos := !readpos&#43;1;
c
with
Invalid_argument "nth&quot; ->
readpos := 0;
fillbuffer ();
get_char ();;

let intbuff = [|0;0;0;0;0;0;0;0;0;0|];;

let get_line () =
let rec aux i =
try
let char = get_char () in
if char = 10 || char = 32 then
i
else
(
intbuff.(i) <- char-48;
aux (i+1)
)
with End_of_file -> i in
aux 0;;

let get_int () =
let linelen = get_line () in
let ib = ref 0 in
for i=0 to linelen - 1 do
ib := !ib*10+;intbuff.(i)
done;
!ib;;

let n = get_int ();;
let k = get_int ();;

let rec count counter = function
0 -> counter
| x -> if (get_int ()) mod k = 0 then count (counter&#43;1) (x-1) else
count counter (x-1);;

print_int (count 0 n);;

exit 0;;

__._,_.___
.

__,_._,___
[1]

about | contact  Other archives ( Real Estate discussion Medical topics )