Re: reducing the time of constructing a List
- To: mathgroup at smc.vnet.net
- Subject: [mg57565] Re: [mg57533] reducing the time of constructing a List
- From: Sseziwa Mukasa <mukasa at jeol.com>
- Date: Wed, 1 Jun 2005 06:02:11 -0400 (EDT)
- References: <200505310900.FAA03434@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
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 Here's one way: Timing[dat /. {0 -> 0, 1 -> 1, 2 -> 0, 3 -> 0, 4 -> 0, 5 -> 0, 6 -> 0, 7 -> 0, 8 -> 0, 9 - > 0, 10 -> 0};] which results in {0.065167 Second, Null} on a 2.7GHz G5 PowerMac. Later, Ssezi