Question: What is the fastest way to obtain a table of values from a parametrized function?
For a function of x
, which is a sum of Sin
functions parametrized by integers k
and m
, one can make a table from the discrete set of values for each parameter:
tabT = Table[Sum[m*Sin[k*(x-n)], {n, 1, 5}], {m, 1, 100}, {k, 1, 50}];
This takes about 0.061s on my machine.
Then one can make a list in x
of this by writing:
tabTT = Table[tabT[[100]][[50]], {x, 1, 100}];
This takes about 3.5s.
Now this process would need to be repeated for each value of k
and m
, so the total time to generate a discrete set for every parameter would be 100*50*3.5s = 17500s.
Alternatively, it is much faster to combine the two steps:
Table[tab[k][m] = Table[Sum[m*Sin[k*(x - n)], {n, 1, 5}], {x, 1, 100}], {m, 1, 100}, {k, 1, 50}];
This takes 4.9s, which is about 3600 times faster.
This can be further improved by using a floating point limit in the sum over n
:
Table[tab[k][m] = Table[Sum[m*Sin[k*(x - n)], {n, 1., 5}], {x, 1, 100}], {m, 1, 100}, {k, 1, 50}];
This now takes 3.5s.
On the other hand, the speed closely depends on the complexity of the function. For example, if a Cos
term is added in parallel to the Sin
term, the evaluation takes twice as long.
What is the best way to build such tables from parametrized functions, especially when the functions become more elaborate? Are there further improvements to this example which can speed up evaluation?