caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Diego Olivier Fernandez Pons <dofp.ocaml@gmail.com>
To: caml-list <caml-list@inria.fr>
Subject: [Caml-list] print_int is too slow
Date: Sun, 24 Jun 2012 20:40:17 +0200	[thread overview]
Message-ID: <CAHqiZ-+6DPuutYdT0dtPZWLR_ePwdjE_UaE0Jzh_WhBDOHb+0w@mail.gmail.com> (raw)

    Team,

I was double checking some OCaml exercises in the France IOI website
(training for the international Olympiads of informatics
http://www.france-ioi.org/)

There is an exercise where my OCaml solution was breaking the time
limit without reason
The issue ended being because print_int seems to be much much slower
than Printf.printf (> 2s for print_int, 0.25s for printf)

The exercise is as follows
A sequence of posters of different lengths L_k are pasted one over the
other (one dimension problem, all starting at the same zero point,
same width, same orientation)
After some time, the program is asked to tell how many posters are still visible

Inputs
- "C %i" means a poster of length x was pasted
- "Q" queries the program for the number of visible posters
Outputs
- number of visible posters after each query

example

input:
12 [number of lines in input]
C 2
Q
C 4
C 2
Q
C 9
Q
C 9
C 2
Q
C 8
Q

output :
1
2
1
2
2

Here is my code

let read_char () = Scanf.scanf " %c " (fun i -> i) in
let read_int () = Scanf.scanf " %i " (fun i -> i) in

let
   length = ref 0 and
   max_queue = ref min_int
in

let rec push v = function
   | [] -> [v]
   | x :: tail when x <= v -> decr length; push v tail (* drop x *)
   | queue -> v :: queue
in

let insert v = function
   | [] | _ when v >= !max_queue ->
      max_queue := v;
      length := 1;
      [v]
   | (x :: _) as queue when v = x -> queue
   | (x :: _) as queue when v < x ->
      incr length;
      v :: queue
   | queue ->
      incr length;
      push v queue
in

let visible = ref [] in

let n = read_int() in
for i = 1 to n do
   match read_char () with
      | 'Q' -> Printf.printf "%i\n" !length
      | 'C' -> visible := insert (read_int()) !visible
      | _ -> failwith "unknown instruction code"
done

If you replace printf by print_int !length; print_newline () the code
is more than 4 times slower.

        Diego Olivier

             reply	other threads:[~2012-06-24 18:40 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-06-24 18:40 Diego Olivier Fernandez Pons [this message]
2012-06-24 18:59 ` Gabriel Scherer
2012-06-24 19:08   ` Diego Olivier Fernandez Pons
2012-06-24 19:18     ` Oliver Bandel
2012-06-24 19:26       ` Diego Olivier Fernandez Pons
2012-06-24 19:48         ` Adrien
     [not found]         ` <20120624.220220.2022399462583597382.Christophe.Troestler@umons.ac.be>
2012-06-24 22:19           ` Diego Olivier Fernandez Pons
2012-06-24 23:06             ` Lukasz Stafiniak
2012-06-24 23:52               ` Anthony Tavener
2012-06-25  0:09               ` Diego Olivier Fernandez Pons
2012-06-25 11:39                 ` oliver
2012-06-25 13:04                   ` Gabriel Scherer
2012-06-25 14:25                     ` oliver
2012-06-25 17:31             ` Florian Weimer
2012-06-25 10:07     ` Goswin von Brederlow

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=CAHqiZ-+6DPuutYdT0dtPZWLR_ePwdjE_UaE0Jzh_WhBDOHb+0w@mail.gmail.com \
    --to=dofp.ocaml@gmail.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).