MathGroup Archive 2005

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

Search the Archive

Re: reducing the time of constructing a List


On May 31, 2005, at 5:00 AM, marloo3 at mail15.com wrote:

> please, what is the best way to reduce the timing of the following  
> lines, the
> source of data is:
> dat = Table[Random[Integer, {0, 10}], {100000}];
> and the purpose is to construct the List lst in the following way:
> For[i = 1, i <= 100000,
>       If [dat[[i]] == 1,
>         lst = Join[lst, {1}], lst = Join[lst, {0}]]; i++]; // Timing
>
> Out[]= {226.07 Second, Null}
> on the P4 celeron 2 Ghz , memory 382 MB ram
> thanks
>

Try this:

       isOne[x_]:= If[ x == 1, 1, 0]

       Map[ isOne, dat ]; // Timing

or in one line:

       If[ # == 1, 1, 0] & /@ dat ; // Timing


----------
Selwyn Hollis
http://www.appliedsymbols.com



  • Prev by Date: Re: opposite of partition
  • Next by Date: Re: reducing the time of constructing a List
  • Previous by thread: Re: reducing the time of constructing a List
  • Next by thread: Re: reducing the time of constructing a List