MathGroup Archive 2005

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

Search the Archive

Re: reducing the time of constructing a List

  • To: mathgroup at smc.vnet.net
  • Subject: [mg57561] Re: reducing the time of constructing a List
  • From: "Carl K. Woll" <carlw at u.washington.edu>
  • Date: Wed, 1 Jun 2005 06:01:55 -0400 (EDT)
  • Organization: University of Washington
  • References: <d7haeo$3pa$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

<marloo3 at mail15.com> wrote in message news:d7haeo$3pa$1 at smc.vnet.net...
> 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
>

You forgot to initialize lst in your post. At any rate, using For is not 
recommended. Instead, try to make use of operations that are listable:

1-Abs[Sign[dat-1]]

evaluates in 0 seconds on my machine and yields the same answer.

Carl Woll 



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