MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: How to compile this module?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg80964] Re: How to compile this module?
  • From: Szabolcs <szhorvat at gmail.com>
  • Date: Thu, 6 Sep 2007 05:33:05 -0400 (EDT)
  • Organization: University of Bergen
  • References: <fbliht$or0$1@smc.vnet.net> <46DE8DAA.3080601@gmail.com>

Szabolcs wrote:
> This function works:
> 
> myC = Compile[{{lst,_Integer,1},{W,_Integer}},
>     Module[{i,j=1,len=Length@lst,ols={}},
>         For[i=1,i<=len,i++,
>             While[
>                 lst[[j]]-lst[[i]]<=W && j<len,
>                 j++
>             ];
>             AppendTo[ols,j-i]
>         ];
>         ols
>     ]]


Oops ... it works but it's damn slow.

Here's a better version:

myM2[lst_,W_]:=
Module[
    {i,j=1,len=Length@lst},
    Table[
        While[lst[[j]]-lst[[i]] <= W && j < len, j++]; j-i,
        {i,1,len}
    ]
]

myC2:=
Compile[{{lst,_Integer,1},{W,_Integer}},
    Module[
        {i,j=1,len=Length@lst},
        Table[
            While[lst[[j]]-lst[[i]] <= W && j < len, j++]; j-i,
            {i,1,len}
        ]
    ]
]

Szabolcs


  • Prev by Date: Re: strange rounding result
  • Next by Date: Re: Problem in Solving Double Integral for PDF transformation
  • Previous by thread: How to compile this module?
  • Next by thread: Re: How to compile this module?