caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Anton Bachin <antronbachin@gmail.com>
To: Chan Ngo <chan.ngo2203@gmail.com>
Cc: OCaml users <caml-list@inria.fr>
Subject: Re: [Caml-list] Constant-time function
Date: Mon, 22 Feb 2016 13:51:07 -0600	[thread overview]
Message-ID: <D81DB0C1-C16D-432F-97BE-160130E2A5E1@gmail.com> (raw)
In-Reply-To: <46EAD496-8667-47FE-B39A-74DA94BC0C25@gmail.com>

1. You are computing the length of l1 and l2 every iteration. On the first iteration, it is the lengths of the original lists. On the second iteration, it is the lengths of their tails. This function takes time quadratic in the maximum of (length l1, length l2), as written. List.length itself takes linear time.

2. If h1 and h2 are nontrivial types, structural equality may take a nontrivial amount of time to compute.

> On Feb 22, 2016, at 13:45, Chan Ngo <chan.ngo2203@gmail.com> wrote:
> 
> Dear all,
> 
> I have write a simple function to compare two lists as below:
> 
> let rec lcomp l1 l2 = 
>  if (List.length l1 != List.length l2) 
>  then false
>  else match l1, l2 with 
>  | h1::t1, h2::t2 -> h1 = h2 && lcomp t1 t2
>  | _, _ -> true;;
> 
> In theory, we can see that the execution is a function of length of l1 and l2 (in case they are same length, otherwise it return immediately) and it is constant time (i.e., we fixed the length of l1 and l2). However, in fact, when I run this function with two lists of around 100 elements, 
> + with only 10th element is different: it takes 0.000027s
> + with only 90th element is different: it takes 0.000116s
> 
> You see the execution times are really different. Thus, I wonder that the compiler did some optimization? Does anyone have some pointers?
> 
> Thanks,
> Chan
> 
> 
>> On Feb 22, 2016, at 4:48 AM, Simon Cruanes <simon.cruanes.2007@m4x.org> wrote:
>> 
>> Hello,
>> 
>> I released bigstring.0.1 yesterday, a small module for dealing with
>> bigarrays of chars. It used to be part of containers, but is useful on
>> its own for low level IO, mirage, etc.; hence the split.  The license is
>> BSD-2-clauses.
>> 
>> Code, issues, etc. can be found at https://github.com/c-cube/ocaml-bigstring ,
>> and contributions and criticism are welcome.
>> 
>> Cheers,
>> 
>> -- 
>> Simon Cruanes
>> 
>> http://weusepgp.info/
>> key 49AA62B6, fingerprint 949F EB87 8F06 59C6 D7D3  7D8D 4AC0 1D08 49AA 62B6
> 
> 
> -- 
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs


  reply	other threads:[~2016-02-22 19:51 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-22  9:48 [Caml-list] [ANN] bigstring 0.1 Simon Cruanes
2016-02-22 19:45 ` [Caml-list] Constant-time function Chan Ngo
2016-02-22 19:51   ` Anton Bachin [this message]
2016-02-22 19:54     ` Anton Bachin
2016-02-22 19:57     ` Chan Ngo
2016-02-22 20:02       ` Anton Bachin
2016-02-22 20:12         ` Milan Stanojević
2016-02-22 20:16           ` Chan Ngo
2016-02-22 20:28             ` Milan Stanojević
2016-02-22 21:25             ` Gerd Stolpmann

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=D81DB0C1-C16D-432F-97BE-160130E2A5E1@gmail.com \
    --to=antronbachin@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=chan.ngo2203@gmail.com \
    /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).