List Info

Thread: "ocaml_beginners"::[] Fast IO




"ocaml_beginners"::[] Fast IO
user name
2006-12-19 13:48:08

2006/12/19, jshaw10 < jshaw10%40yahoo.com">jshaw10yahoo.com>:
>; Once again I've hit a barrier and can't make mine work any faster.
&gt; Using Pervasives.input to get 4096 bytes at a time is actually slower
&gt; 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 and scored 4.38 seconds.

My code score 1.10 (well 0.92 now), and use input.

My comment on your code: firstly my code is way shorter. May be the
code generated is smaller also, and do fit better in the cache. It is
also made of only one recursive function, that do everything
(transformation to int, reading, well everything). I also just gain
0.20 second by using String.unsafe_get q i in place of s.[i]

[...]

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

Well, I believe the main difference is here: when reading a line, I
directly transform it to the int, without filling any intermediate
buffer. I also use no exception. Note that input don't raise
End_of_file, but return 0 when at the end of the file. You might be
loosing some time here. Note that ocaml array access are protected, if
you want raw speed, you want to use unsafe function (as you are
already protecting yourself in you code).

Note that my code is made for raw speed, and I won't wrote it for
anything but this kind of contest. It is realy a bad style coding, but
efficient.

__._,_.___
.

__,_._,___
"ocaml_beginners"::[] Fast IO
user name
2006-12-19 15:58:50

Thanks for your suggestions! I got rid of the intermediate buffer for
making ints (totally useless, you were 100% right there) and got my
time down quite a bit. I'll try to get rid of some other code and see
what happens.

__._,_.___
.

__,_._,___
[1-2]

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