caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Issac Trotts <ijtrotts@ucdavis.edu>
To: OCaml Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] function
Date: Fri, 6 Dec 2002 13:31:21 -0800	[thread overview]
Message-ID: <20021206213120.GC696@boson.ucdavis.edu> (raw)
In-Reply-To: <200212051800.55361.oleg_inconnu@myrealbox.com>


Oleg wrote:
> On Thursday 05 December 2002 03:24 pm, Issac Trotts wrote:
> >   List.concat (List.map to_list2 strs);;
> 
>     ^^^^^^^^^
> 
> > > 2)  What is the complexity of your function f ?
> >
> > The new ones have linear time complexity w.r.t. the number of
> > characters.  The old one has quadratic time complexity.
> 
> Issac
> 
> I haven't analyzed your whole functions, but List.flatten'ing alone is 
> O(S*L^2), so while it may be linear WRT the number of characters in one 
> string, it's not necessarily linear WRT the total number of characters. See 
> my version of f for O(L*S) time.
> 
> Cheers
> Oleg

Thanks for pointing this out.

Here's flatten, from list.ml:

  let rec flatten = function
      [] -> []
    | l::r -> l @ flatten r

  let concat = flatten

Here's (@), from pervasives.ml : 

  let rec (@) l1 l2 =
    match l1 with
      [] -> l2
    | hd :: tl -> hd :: (tl @ l2)

If the given list has L elements, each with S items, then flatten should 
O((L*S)*L) = O(S*L^2) time, since you have to keep on churning through
every single element in the ever-expanding l at every recursive flatten 
call.  That's too bad.

Here's an experiment I tried:

$ cat ftime.ml 

  let rec make_list_of_lists = function 
    | 0 -> []
    | n -> [1;2;3;4;5;6;7;8] :: make_list_of_lists(n-1)

  let _ = 
    let n = int_of_string(Sys.argv.(1)) in
    let lol = make_list_of_lists n in
    ignore(List.flatten(lol))
  ;;

$ ocamlopt -o ftime ftime.ml
$ for (( i=0; $i<100; i++ )) 
  do 
    /usr/bin/time -o data -a -f "%U" ./ftime "$i"000  
  done 
$ gnuplot
  > plot 'data'

I guess it looks linear because of the small input size.
This gives me the impression that List.flatten is practical,
at least for small data sets.

Issac


-------------------
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:[~2002-12-06 21:30 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-12-02 15:55 altavillasalvatore
2002-12-02 16:29 ` sebastien FURIC
2002-12-02 17:35 ` Oleg
2002-12-03 23:22 ` Issac Trotts
2002-12-05 10:00   ` Pierre Weis
2002-12-05 20:24     ` Issac Trotts
2002-12-05 23:00       ` Oleg
2002-12-06 21:31         ` Issac Trotts [this message]
2002-12-07 10:28           ` Xavier Leroy
2002-12-07 17:31             ` Oleg
2002-12-05 20:46     ` Oleg
2002-12-05 21:06       ` Pal-Kristian Engstad
2002-12-06  7:38 Jeremy Fincher

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=20021206213120.GC696@boson.ucdavis.edu \
    --to=ijtrotts@ucdavis.edu \
    --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).