How Do I Make My Mathematica Programs Run Faster?
Author
Wolfram Research, Inc.
Title
How Do I Make My Mathematica Programs Run Faster?
Description
Use the Newest Version Use Built-in Functions Use Machine-Precision Numbers Avoid Procedural Programming Operate on Lists as a Whole, Not on Individual Parts Use Compile Use the Parallel Computing Toolkit Use MathCode C++ Also available in Japanese http://library.wolfram.com/howtos/faster/index.ja.html
Category
Other
Keywords
URL
http://www.notebookarchive.org/2018-10-10ra1uu/
DOI
https://notebookarchive.org/2018-10-10ra1uu
Date Added
2018-10-02
Date Last Modified
2018-10-02
File Size
29.16 kilobytes
Supplements
Rights
Redistribution rights reserved




How do I make my Mathematica programs run faster?
How do I make my Mathematica programs run faster?
Use the newest version
Use the newest version
Use built-in functions
Use built-in functions
Use machine precision numbers
Use machine precision numbers
Avoid procedural programming
Avoid procedural programming
Operate on lists as a whole, not on individual parts.
Operate on lists as a whole, not on individual parts.
Mathematica is able to operate on whole lists at once, or using the new PackedArray technology, even on lists in an internal compressed form. This is much faster than stepping through lists calculating single elements. Also, keep in mind list manipulation functions like Partition.
This effect is already quite significant for simple operations like taking the sine of all elements in a list.
This effect is already quite significant for simple operations like taking the sine of all elements in a list.
In[]:=
Timing[Table[Sin[x],{x,0,π,π/10000.}];]
Out[]=
{1.21Second,Null}
In[]:=
Timing[Sin[Range[0,π,π/10000.]];]
Out[]=
{0.22Second,Null}
For a more realistic example, lets say we have two vectors of x and y values which we want to combine into a list of {x,y} pairs.
In[]:=
a=Table[Random[],{10000}];b=Table[Random[],{10000}];
A pretty straightforward approach would be constructing a new table.
In[]:=
Timing[newtable=Table[{a[[i]],b[[i]]},{i,1,Length[a]}];]
Out[]=
{19.49Second,Null}
In[]:=
newtable[[1]]
Out[]=
{0.0672707,0.2083}
Combining the lists, transposing and partitioning them gives the same result, but is infinitely faster.
In[]:=
Timing[newtablefast=Transpose[{a,b}];]
Out[]=
{0.Second,Null}
In[]:=
newtablefast[[1]]
Out[]=
{0.665766,0.312107}
Use Compile
Use Compile
Use the Parallel Computing Toolkit
Use the Parallel Computing Toolkit
Use MathCode
Use MathCode


Cite this as: Wolfram Research, Inc., "How Do I Make My Mathematica Programs Run Faster?" from the Notebook Archive (2002), https://notebookarchive.org/2018-10-10ra1uu

Download

