https://wiki.haskell.org/api.php?action=feedcontributions&user=JCAB&feedformat=atomHaskellWiki - User contributions [en]2019-10-22T07:19:10ZUser contributionsMediaWiki 1.27.4https://wiki.haskell.org/index.php?title=Programming_performance/JCAB_Cpp&diff=11977Programming performance/JCAB Cpp2007-03-15T06:45:35Z<p>JCAB: </p>
<hr />
<div>* '''Language''': C++<br />
* '''Skill''': Advanced. I've been using C++ with all its features for many years.<br />
* '''Time''': Approx 20 minutes.<br />
* '''Notes''': I did this after doing the Haskell version, so that might have affected the time. I used Visual Studio 2003 for development. I needed two iterations (same as with Haskell, but different bugs!). I get 13754.97774 just like I did with Haskell.<br />
<br />
== Code ==<br />
<pre-c><br />
#include <stdio.h><br />
#include <ctype.h><br />
#include <list><br />
<br />
<br />
int main()<br />
{<br />
std::list<double> closingCosts;<br />
FILE* f = fopen("C:\\JCAB\\ProgrammingPerformance\\gspc.txt", "rt");<br />
if (f) {<br />
while (!feof(f)) {<br />
char buf[4096];<br />
fgets(buf, sizeof(buf), f);<br />
buf[sizeof(buf)-1] = 0;<br />
<br />
if (buf[0] != '#') {<br />
char* p = buf;<br />
while (isspace(*p)) ++p;<br />
while (*p && !isspace(*p)) ++p;<br />
while (isspace(*p)) ++p;<br />
while (*p && !isspace(*p)) ++p;<br />
while (isspace(*p)) ++p;<br />
while (*p && !isspace(*p)) ++p;<br />
while (isspace(*p)) ++p;<br />
while (*p && !isspace(*p)) ++p;<br />
while (isspace(*p)) ++p;<br />
while (*p && !isspace(*p)) ++p;<br />
while (isspace(*p)) ++p;<br />
while (*p && !isspace(*p)) ++p;<br />
while (isspace(*p)) ++p;<br />
<br />
closingCosts.push_front(atof(p));<br />
}<br />
}<br />
fclose(f);<br />
}<br />
<br />
double cash = 10000.0;<br />
std::list<std::pair<double, double> > shares;<br />
<br />
std::list<double>::const_iterator it = closingCosts.begin();<br />
double lastClosing = *it;<br />
++it;<br />
<br />
std::list<double>::const_iterator const closingCostsEnd = closingCosts.end();<br />
for (; it != closingCostsEnd; ++it) {<br />
double const currentClosing = *it;<br />
<br />
for (std::list<std::pair<double, double> >::iterator sIt = shares.begin(); sIt != shares.end();) {<br />
if (currentClosing - sIt->first >= 0.06 * sIt->first) {<br />
// Sell them.<br />
cash += sIt->second * currentClosing;<br />
sIt = shares.erase(sIt);<br />
} else {<br />
++sIt;<br />
}<br />
}<br />
<br />
if (lastClosing - currentClosing > 0.03 * lastClosing) {<br />
// Buy some.<br />
shares.push_back(std::make_pair(currentClosing, cash * 0.10 / currentClosing));<br />
cash *= 0.90;<br />
}<br />
<br />
lastClosing = currentClosing;<br />
}<br />
<br />
// Sell all that's left.<br />
for (std::list<std::pair<double, double> >::iterator sIt = shares.begin(); sIt != shares.end(); ++sIt) {<br />
cash += sIt->second * lastClosing;<br />
}<br />
<br />
printf("%f\n", cash);<br />
<br />
return 0;<br />
}<br />
</pre-c></div>JCABhttps://wiki.haskell.org/index.php?title=Programming_performance&diff=11976Programming performance2007-03-15T06:38:48Z<p>JCAB: </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 />
<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>JCABhttps://wiki.haskell.org/index.php?title=Programming_performance/JCAB_Haskell&diff=11975Programming performance/JCAB Haskell2007-03-15T06:09:46Z<p>JCAB: </p>
<hr />
<div>* '''Language''': Haskell<br />
* '''Skill''': Intermediate. I don't use Haskell much, and I keep going back to the library documentation for the simplest of functions, but I believe I understand many of the scaries aspects of the language.<br />
* '''Time''': Approx 50 minutes.<br />
* '''Notes''': This is a bit of let-overkill, but to me it makes sense this way. I didn't see any of the other submissions prior to doing this. I used Visual Haskell for development. I needed two iterations (silly bugs!). I get 13754.97774 as the answer, which sounds more-or-less right (37% gain). It would be nice to know the (a?) correct answer ahead of time, so we know when we got the program right. The problem wasn't very clearly described, IMHO.<br />
<br />
== Code ==<br />
<haskell><br />
module Main where<br />
<br />
programmingPerformance file =<br />
let filteredFile = filter filterLine (lines file)<br />
filterLine [] = False<br />
filterLine ('#':_) = False<br />
filterLine _ = True<br />
<br />
closingList = reverse (map extractClosing filteredFile) :: [Double]<br />
extractClosing line = read ((tokenize line) !! 6)<br />
tokenize [] = []<br />
tokenize line = let (token, rest) = getToken line in token : tokenize rest<br />
getToken [] = ("", [])<br />
getToken (' ':xs) = ("", xs)<br />
getToken (x :xs) = let (token, rest) = getToken xs in (x:token, rest)<br />
<br />
result = parseClosingSell 10000.0 [] closingList<br />
parseClosingSell :: Double -> [(Double, Double)] -> [Double] -> Double<br />
parseClosingSell cash shares c@(_:y:rest) =<br />
let (sale, remShares) = foldr maybeSell (0, []) shares<br />
maybeSell (shClosing, shNum) (accCash, accShares) | (y - shClosing) >= shClosing * 0.06 = (accCash + y * shNum, accShares)<br />
maybeSell sh (accCash, accShares) = (accCash , sh:accShares)<br />
in parseClosingBuy (cash + sale) remShares c<br />
parseClosingSell cash shares [y] =<br />
let sale = foldr sell 0 shares<br />
sell (shClosing, shNum) accCash = accCash + y * shNum<br />
in cash + sale<br />
parseClosingBuy cash shares (x:y:rest) | (x - y) >= x * 0.03 = parseClosingSell (cash * 0.90) ((y, cash*0.10 / y):shares) (y:rest)<br />
parseClosingBuy cash shares (x:rest) = parseClosingSell cash shares rest<br />
<br />
in result<br />
<br />
main = do<br />
file <- readFile "C:\\JCAB\\ProgrammingPerformance\\gspc.txt"<br />
let result = programmingPerformance file<br />
putStrLn (show result)<br />
</haskell></div>JCABhttps://wiki.haskell.org/index.php?title=Programming_performance&diff=11974Programming performance2007-03-15T06:00:35Z<p>JCAB: </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 />
<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>JCAB