caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Markus Mottl <markus@oefai.at>
To: Siegfried Gonzi <siegfried.gonzi@stud.uni-graz.at>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Reading a file
Date: Thu, 22 May 2003 12:26:54 +0200	[thread overview]
Message-ID: <20030522102654.GA23001@fichte.ai.univie.ac.at> (raw)
In-Reply-To: <3ECC6DD5.9050509@stud.uni-graz.at>

Siegfried Gonzi schrieb am Donnerstag, den 22. Mai 2003:
> Thanks for this version. Without any insult, it works well and takes for
> this legendary file now 25 seconds, though, I find the version ugly. The
> Clean, Bigloo, Fortran 95 and C++ version more or less takes the same time.

There is always room for making things faster or shorter: it depends on
what you prefer.

If you want to have an even faster version, you could use float arrays
instead of float lists. E.g. instead of this line in the previous
solution:

  | Some line -> loop (List.map my_frob (split line del) :: acc)

Use this:

  | Some line ->
      let str_ar = Array.of_list (split line del) in
      loop (Array.map my_frob str_ar :: acc)

This is probably what you really need anyway.

If you want to have a short version, which is still pretty fast, use this:

---------------------------------------------------------------------------
let frob userval = function
  | "n/a" | "nil" -> userval
  | s -> float_of_string s
;;

let extract_floats file del nan_proxy =
  let rex = Str.regexp_string (String.make 1 del) in
  let my_frob = frob nan_proxy in
  let acc_ref = ref [] in
  (try
    while true do
      let line = input_line file in
      let str_ar = Array.of_list (Str.split rex line) in
      acc_ref := Array.map my_frob str_ar :: !acc_ref
    done
  with End_of_file -> ());
  List.rev !acc_ref
;;

let f = open_in "/home/gonzi/test.txt";;
let erg2 = extract_floats f ',' (-1.0);;
let rows = List.length erg2;;
rows;;
---------------------------------------------------------------------------

> But there remains my requirement for a better OCaml solution.

Better = faster or smaller...?

> 1) Convert the string-line to a character-list: [x\\x<-: string-line]

This would be _very_ inefficient.

> This, oh dear Watson is what I mean with elegance. No need of string
> indexing counting or other error prone stuff.

Use regular expressions: fast enough and very short.

> So, this must also be possible in OCaml - or must not?

You cannot just take one solution of one language and assume that it
is equally efficient or elegant in another language. This also heavily
depends on availability and functionality of libraries. E.g. there is no
"take_while" or "drop_while" in the OCaml standard library - you'd have
to write them yourself.

Regards,
Markus Mottl

-- 
Markus Mottl          http://www.oefai.at/~markus          markus@oefai.at

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


  reply	other threads:[~2003-05-22 10:26 UTC|newest]

Thread overview: 63+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <ocaml@tagger.yapper.org>
2003-03-31 16:51 ` [Caml-list] How can I check for the use of polymorphic equality? Neel Krishnaswami
2003-03-31 17:33   ` brogoff
2003-04-03 19:44   ` Jason Hickey
2003-04-03 20:40     ` Pierre Weis
2003-04-03 20:53       ` Chris Hecker
2003-04-04  8:46         ` Pierre Weis
2003-04-04 19:05           ` Jason Hickey
2003-04-04  9:10         ` Andreas Rossberg
2003-05-14 11:43 ` [Caml-list] ocaml and large development projects Traudt, Mark
2003-05-14 15:52   ` Jason Hickey
2003-05-18  5:32     ` Chris Hecker
2003-05-18  5:44       ` David Brown
2003-05-18  6:10         ` Chris Hecker
2003-05-18 11:13           ` John Carr
2003-05-18 16:51             ` Ed L Cashin
2003-05-18 18:08               ` Lex Stein
2003-05-18 19:08                 ` Ed L Cashin
2003-05-18 19:55                   ` Lex Stein
2003-05-19  8:13                   ` Markus Mottl
2003-05-19  8:33                     ` Nicolas Cannasse
2003-06-02 21:59                     ` John Max Skaller
2003-05-18 23:19                 ` Chris Hecker
2003-05-18 14:38           ` David Brown
2003-05-18 16:00             ` Ville-Pertti Keinonen
2003-05-19 15:36           ` Brian Hurt
2003-05-19 19:31             ` Chris Hecker
2003-05-19 23:39               ` Seth Kurtzberg
2003-05-20  8:07               ` [Caml-list] ocaml as *.so (was: ...and large development projects) Wolfgang Müller
2003-05-20  8:42                 ` [Caml-list] Reading a file Siegfried Gonzi
2003-05-20 10:21                   ` Mattias Waldau
2003-05-20 10:48                   ` Nicolas Cannasse
2003-05-20 10:55                   ` Markus Mottl
2003-05-20 13:20                   ` Michal Moskal
2003-05-20 12:21                     ` Siegfried Gonzi
2003-05-21  6:11                     ` Siegfried Gonzi
2003-05-21  6:48                       ` Siegfried Gonzi
2003-05-21  6:53                         ` Siegfried Gonzi
2003-05-21  9:16                           ` Markus Mottl
2003-05-21 10:04                             ` Eray Ozkural
2003-05-21 16:20                               ` brogoff
2003-05-21  8:21                       ` Michal Moskal
2003-05-21  7:24                         ` [Caml-list] PsiLAB works fine under Linux SuSE 8 Siegfried Gonzi
2003-05-21  9:11                       ` [Caml-list] Reading a file Markus Mottl
2003-05-22  6:27                         ` Siegfried Gonzi
2003-05-22 10:26                           ` Markus Mottl [this message]
2003-05-23  5:59                             ` Siegfried Gonzi
2003-05-23  6:04                               ` Siegfried Gonzi
2003-05-20 10:45                 ` [Caml-list] ocaml as *.so (was: ...and large development projects) Nicolas Cannasse
2003-05-20 11:17                   ` Wolfgang Müller
2003-05-20 11:31                     ` Nicolas Cannasse
2003-05-20 11:40                       ` Wolfgang Müller
2003-06-02 22:40                 ` John Max Skaller
2003-06-03 13:26                   ` [Caml-list] ocaml as *.so Remi Vanicat
2003-06-02 22:42               ` [Caml-list] ocaml and large development projects John Max Skaller
2003-06-02 21:24           ` John Max Skaller
2003-06-02 21:12       ` John Max Skaller
2003-06-03  0:31         ` Chris Hecker
2003-06-03 10:13           ` Michal Moskal
2003-06-03 18:12             ` Chris Hecker
2003-06-03 14:31           ` art yerkes
2003-06-03 21:55           ` Jason Hickey
2003-06-03 22:42             ` Chris Hecker
2003-06-06 23:46             ` John Max Skaller

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=20030522102654.GA23001@fichte.ai.univie.ac.at \
    --to=markus@oefai.at \
    --cc=caml-list@inria.fr \
    --cc=siegfried.gonzi@stud.uni-graz.at \
    /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).