Programming performance/RaymondH Python

From HaskellWiki
< Programming performance
Revision as of 03:15, 8 March 2007 by Rhettinger (talk | contribs) (Add results)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

This took about 20 minutes to write with plain vanilla Python,

<code-python> data = [] for line in open('gspc.txt'):

   if not line.startswith('#'):
       data.append(float(line.split()[-1]))

data.reverse()

purchases = {} # price --> quantity at that price cash = 10000.00 for prev, close in zip(data, data[1:]):

   change = close / prev
   if change <= 0.97:
       invest = cash * 0.10
       cash -= invest
       purchases[close] = purchases.get(close, 0) + invest / close
   for pprice, qty in purchases.items():
       if close >= pprice * 1.06:
           cash += qty * close
           del purchases[pprice]

cash += sum(purchases.values()) * close print cash </code-python>