![Bookmark and Share](http://s7.addthis.com/static/btn/v2/lg-share-en.gif)
Mathematica is functional and has high code density. It tends to
lead one to use more powerful function than the task really
necessarily requires. Because high code density typically means it
needs to check many things to figure out default (or
Automatic
) values, it slows down the program
unnecessarily sometimes.
Join
vs. Flatten
Use Join
to join lists, not Flatten, if you know
it's safe. Join
is faster and uses less memory.
In[6]:= Quit[]
In[1]:= data=Table[RandomVariate[NormalDistribution[],100],{20}];
In[2]:= m1=MemoryInUse[];
Do[t=Flatten[data,1],{10000}]//AbsoluteTiming
m2=MemoryInUse[];
m2-m1
Out[3]= {3.9662268,Null}
Out[5]= 74704
Compare it to
Join@@list
In[2]:= m1=MemoryInUse[];
Do[t=Join@@data,{10000}]//AbsoluteTiming
m2=MemoryInUse[];
m2-m1
Out[3]= {0.1730099,Null}
Out[5]= 36240