https://wiki.haskell.org/api.php?action=feedcontributions&user=Christoph&feedformat=atomHaskellWiki - User contributions [en]2021-04-16T20:29:25ZUser contributionsMediaWiki 1.27.4https://wiki.haskell.org/index.php?title=Programming_performance&diff=14748Programming performance2007-07-30T14:51:32Z<p>Christoph: </p>
<hr />
<div>[[Category:Code]]<br />
<br />
== Goal ==<br />
<br />
The goal is to measure how long it takes to implement solutions to a standard problem in your programming language of choice and to see the various approaches different programmers take when implementing the solution. The goal isn't to finish the problem as quickly as possible but to rather to get some realistic measurements on programming speed and approaches.<br />
<br />
== Submitted results ==<br />
<br />
{| border="1" cellspacing="0" cellpadding="5" align="center" <br />
! Data<br />
! Language<br />
! Experience<br />
! Time<br />
|-<br />
| [[Programming performance/TimN Haskell]]<br />
| Haskell<br />
| Intermediate<br />
| 45min?<br />
|-<br />
| [[Programming performance/TimN Python]]<br />
| Python<br />
| Advanced<br />
| 30min?<br />
|-<br />
| [[Programming performance/Magnus Haskell]]<br />
| Haskell<br />
| Intermediate<br />
| 25min<br />
|-<br />
| [[Programming performance/kc5tja Forth]]<br />
| Forth<br />
| Advanced<br />
| 3.7hr<br />
|-<br />
| [[Programming performance/Apter K]] (added by kc5tja)<br />
| K<br />
| Advanced<br />
| 15min?<br />
|-<br />
| [[Programming performance/hay.steve Python]]<br />
| Python<br />
| Beginner<br />
| 1.5hr<br />
|-<br />
| [[Programming performance/JasonWoof Ruby]]<br />
| Ruby<br />
| Beginner<br />
| 50 minutes<br />
|-<br />
| [[Programming performance/RaymondH Python]]<br />
| Python<br />
| Advanced<br />
| 20-25 minutes<br />
|-<br />
| [[Programming performance/ArthurVanLeeuwen Haskell]]<br />
| Haskell<br />
| Advanced<br />
|25 minutes<br />
|-<br />
| [[Programming performance/JasonWoof gforth]]<br />
| gforth<br />
| Experienced<br />
|102 minutes<br />
|-<br />
| [[Programming performance/ScottN MySQL]]<br />
| MySQL<br />
| Advanced<br />
|23 minutes<br />
|-<br />
| [[Programming performance/JN K]]<br />
| K<br />
| Intermediate<br />
|17 minutes<br />
|-<br />
| [[Programming performance/JCAB Haskell]]<br />
| Haskell<br />
| Intermediate<br />
|50 minutes<br />
|-<br />
| [[Programming performance/JCAB Cpp]]<br />
| C++<br />
| Advanced<br />
|20 minutes<br />
|-<br />
| [[Programming performance/KrassiVanguelov R]]<br />
| R<br />
| Advanced<br />
|22 minutes<br />
|-<br />
| [[Programming performance/Christoph Haskell]]<br />
| Haskell<br />
| Intermediate<br />
|30 minutes<br />
|}<br />
<br />
<br />
<br />
<br />
== Instructions ==<br />
<br />
Implement a program that computes the solution to the following problem in your language of choice. Measure how long it took you to make your initial implementation. Post the code and the amount of time it took. Please submit only your time and code for your first implementation in a given language. If you have implemented a solution previously in another language please mention that. You may submit further refinements of your solution or new solutions on the same page as your initial submission but the primary goals is to measure your initial solution and the amount of time required. If you wish, you may look at other solutions but it is preferable that you do not.<br />
<br />
Submit your entry by creating a wiki page with your code, your time, the language used and a note about how comfortable you are with this language. If you relied on any help, such as reading other solutions, please state what sort of help you made use of. Please make just one page per participant per language. You may include any comments, refinements or alternate programs on this same page.<br />
<br />
<br />
<br />
<br />
== Problem ==<br />
<br />
Simulate the following strategy on the GSPC data set:<br />
<br />
* Whenever the closing cost is down 3% or more from the previous closing cost, buy shares with 10% of current cash.<br />
* Whenever the closing cost is up 6% or more from the original purchase price, sell back the full amount of stock purchased at that price.<br />
* Sell off any remaining assets after the last day.<br />
<br />
<br />
Run the strategy on the provided data starting with $10,000.00 in cash and no holdings.<br />
* All purchases are made at the closing cost.<br />
* Fractional shares may be purchased.<br />
* Fractional pennies may be used.<br />
* Do not simulate any trading costs, interest dividends or taxes.<br />
* Percent change is 100 percent times the change (new price minus old price) divided by the old price.<br />
<br />
<br />
=== Data ===<br />
<br />
The http://www.thenewsh.com/gspc.txt file contains the test data. Any lines not starting with a hash sign ("#") contain valid data. Data is in space seperated columns. Each line represents one day's data with most recent date first. The first column contains the date and the last column contains the closing price. The number of columns is always the same.<br />
<br />
<br />
=== Results ===<br />
<br />
Display the amount of cash held after executing the strategy. A mode that displays each buy and sell by date is useful, but optional.</div>Christophhttps://wiki.haskell.org/index.php?title=Programming_performance/Christoph_Haskell&diff=14747Programming performance/Christoph Haskell2007-07-30T14:51:16Z<p>Christoph: </p>
<hr />
<div>* '''Language''': Haskell<br />
* '''Skill''': Intermediate. I learned Haskell some months ago but didn't use it for a serious project so far.<br />
* '''Time''': 30 minutes<br />
<br />
== Code ==<br />
<haskell><br />
module Main where<br />
<br />
data Values = Values { money, lastCosts :: Double<br />
, stock :: [(Double, Double)] }<br />
<br />
readClosingCosts :: String -> Double<br />
readClosingCosts = read . last . words<br />
<br />
notComment ('#':_) = False<br />
notComment _ = True<br />
<br />
readCosts :: IO [Double]<br />
readCosts = readFile "gspc.txt" >>=<br />
(return . map readClosingCosts . filter notComment . lines)<br />
<br />
buy :: Values -> Double -> Double -> Values<br />
buy v c amount = Values { money = (1 - amount) * money v<br />
, stock = (amount * (money v) / c, c) : stock v<br />
, lastCosts = c }<br />
<br />
sellAll costs (bought, _) = bought * costs<br />
<br />
sellFinally :: Double -> Values -> Double<br />
sellFinally costs v = money v + (sum $ map (sellAll costs) (stock v))<br />
<br />
tooMuchUp costs (bought, origCosts) = costs >= 1.06 * origCosts<br />
<br />
step1 costs v<br />
| costs <= 0.97 * lastCosts v = buy v costs 0.1<br />
| otherwise = v { lastCosts = costs }<br />
<br />
step2 costs v = v { money = money v + newMoney<br />
, stock = filter (not . tooMuchUp costs) (stock v) }<br />
where newMoney = sum $ map (sellAll costs)<br />
(filter (tooMuchUp costs) (stock v))<br />
<br />
step :: Double -> Values -> Values<br />
step costs v = step2 costs (step1 costs v)<br />
<br />
main = do<br />
costs <- readCosts<br />
print $ sellFinally (head costs) $ foldr step (Values 10000 0 []) costs<br />
</haskell></div>Christoph