Timing of a simple table operation
- To: mathgroup at yoda.physics.unc.edu
- Subject: Timing of a simple table operation
- From: Jeffrey K. MacKie-Mason <jmm at umich.edu>
- Date: Fri, 7 Aug 92 13:19:40 EDT
I created a list of numbers with Table, then calculated the product
of 1 plus each element in the list. I want to calculate many such
products, so I nested this inside another Table, but found that it
took *forever* to calculate. I can illustrate this but having the
outer Table create only one product of an inner Table, and then do
the same thing without the outer Table. The Table-nested approach
tool 5 minutes (NeXT '040-25mHz), the piecemeal approach took about
0.3 seconds!
I'm pretty new to Mma, and I understand recursion is a tricky beast,
but since I can create this massive delay when the outer Table is
only one element long I'm baffled. Suggestions?
home/jmm.12> math
Mathematica 2.0 for NeXT
Copyright 1988-91 Wolfram Research, Inc.
-- NeXT graphics initialized --
In[1]:= <<Statistics`ContinuousDistributions`
In[2]:= rdist = NormalDistribution[(1.1^(1/240)-1),(0.2/240)]
Out[2]= NormalDistribution[0.000397205, 0.000833333]
In[3]:= annualReturns=Timing[Table[
(Product[
(1+Table[Random[rdist],{240}][[i]]),
{i,1,240}
] - 1),
{1}
]]
Out[3]= {312.933 Second, {0.109928}}
In[4]:= y = Timing[Table[Random[rdist],{240}]][[2]]
Out[4]= {-0.000684265, < list of 240 numbers suppressed> }
In[5]:= Timing[Product[(1+y[[i]]),{i,1,240}]-1]
Out[5]= {0.266667 Second, 0.114892}
So, the nested Tables took 5 minutes, the direct (two-step) approach
less than 0.3 seconds.
What dumb thing am I doing?
---
Prof. Jeff MacKie-Mason internet: jmm at umich.edu
Dept. of Economics bitnet: usergc76 at umichum
Univ. of Michigan phone: 313-764-7438
Ann Arbor, MI 48109-1220 fax: 313-763-9181