In general, I think there are two methods of evaluating a variable in Mathematica. My question is about performance of the two methods.
Suppose that our final goal is to evaluate a1
.
We know that
a1
is defined by b1,b2,b3
b1
is defined by c1,c2,c3
b2
is defined by c4,c5,c6
b3
is defined by c7,c8,c9
c1
is defined by d11,d12,d13
c2
is defined by d21,d22,d23
...
c9
is defined by d91,d92,d93
If we evaluate a1
, then mathematica's evaluating process is
- seeks for definition of
a1
.
Change the expression ofa1
usingb1,b2,b3
- seeks for definition of
b1,b2,b3
Change the expression ofa1
usingc1,c2,..,c9
. - seeks for definition of
c1,c2,..,c9
change the expressiona1
usingd11,d12,...,d93
- finally evaluate
a1
Let's call the above method alpha-method.
But we could use other method, namely beta-method :
- Evaluate
c1,c2,...,c9
, - Evaluate
b1,b2,b3
- Evaluate
a1
The difference of alpha-method and beta-method is that,
for beta-method, there is no work of seeking definitions or changing variables of a expression of a1
.
Moreover, we could save some memories like :
- Evaluate
c1,c2,...,c9
, cleard11,d12,...,d93
- Evaluate
b1,b2,b3
, clearc1,c2,..,c9
- Evaluate
a1
.
So I guess that beta-method is faster and more memory-saving then alpha-method.
I saw some elegant solutions for some questions(especially question about function-constructing), which can be classified into alpha-method. Define X
using some variables, and define those variables with another variables,... then we see X
is completely determined.
A code of alpha-method usually contains just few definitions.
A code of beta-method usually contains many conditionals and loop commands.
Is it true(or worth considering) that beta-method should be used in case the performance is important?