Re: Re: fast way of appending x zeros to a list?
- To: mathgroup at smc.vnet.net
- Subject: [mg79178] Re: [mg79106] Re: fast way of appending x zeros to a list?
- From: Murray Eisenberg <murray at math.umass.edu>
- Date: Fri, 20 Jul 2007 03:29:24 -0400 (EDT)
- Organization: Mathematics & Statistics, Univ. of Mass./Amherst
- References: <f7ke8n$508$1@smc.vnet.net> <200707190721.DAA01175@smc.vnet.net>
- Reply-to: murray at math.umass.edu
Why not use Join or PadRight instead of Do? They seem to be faster.
Here are timings in Mathematica 6.0.1 (under Windows XP):
data1=data2=data3=data=Table[RandomReal[],{100000}];
k=100000;
(Do[data1={0,data1},{k}];Flatten[data1];)//Timing
data2=Join[data,Table[0,{k}]];//Timing
data3=PadRight[data,k];//Timing
{0.156,Null}
{0.031,Null}
{0.016,Null}
Peter Breitfeld wrote:
> kristoph schrieb:
>> Dear all,
>>
>> I'm looking for a fast way to append 0's to a list. Unfortunately, I
>> append a variable number of 0's to a list which is generated by a
>> For[...]-loop. Therefore, PadLeft[...] is not variable enough to
>> append exactly x zeros to the list.
>>
>> At the moment I use: Do[PrependTo[data,0],{K}] where the K depends on
>> certain factors.
>
> AppendTo and PrependTo are known to be very slow. It's better to
> build up a nested list an Flatten it:
>
> Do[data={0,data},{K}]; Flatten[data]
>
> Gruss Peter
--
Murray Eisenberg murray at math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower phone 413 549-1020 (H)
University of Massachusetts 413 545-2859 (W)
710 North Pleasant Street fax 413 545-1801
Amherst, MA 01003-9305
- References:
- Re: fast way of appending x zeros to a list?
- From: Peter Breitfeld <phbrf@t-online.de>
- Re: fast way of appending x zeros to a list?