Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Part of me wants to do that just for the sake of discussion and open source, etc etc. But the other part of me is paranoid about it :)

The general idea of both sets of code is pretty much exactly the same - that is what is so nice about Perl - it translates nearly exactly over to C when you are writing it.

The differences are in split in Perl and strsep in C (although I think that might be a BSD biased thing and not ANSI - that said, Linux has it too) - which lead to very very slight ways of how I get that info out.
In perl I have to split, then on the array that it returns I grab the known positions that I want. In C I have to iterate on calls to strsep to the spot I want and then it returns a string (or pointer to a string).

In C I prepend increments (++i) and in perl I postpend (i++) if they are on lines by themselves since I read that C is slightly more efficient with the former.

For the most part though, it is exactly the same, only differing where Perl and C don't have matching built in functionality.

Again, in Perl I can do:
$strWhatever = $strA . ',' . $strB . ',' $strC;
(that probably being a great example of some inefficiency that another response on this thread alluded to me likely making - I would guess that the above is probably like in Java how it is better to append to a stringBuffer instead of creating many strings and having it deal with it all)
In C I have to strcat the series together.


The code in general, in ugly not-even-pseudo-code (I'll try to note in their respective comments how Perl or C is treated differently - perhaps shedding light on what is causing the Perl slowdown... aside from it being an interpreted language and all):


if(fileLength >= 1200 rows)
arrClosingDayData = grabTicker(TickerName)
#in Perl this loads it right into an array, and later splits on individual spots in the array to grab certain spots of the csv and assigns it to a different array
#open(TICKERDATA, $tickerName) or die "Can't open TICKERDATA: $!\n";
#my @allTickerData = <TICKERDATA>;
#close(TICKERDATA) or die "Can't close TICKERDATA: $!\n";

/*in C it is a string and then that string is iterated along newlines, we then strsep the data up based on the ",", grabbing the data out that we want, and populating an array of known size with those float values*/

Then we generate a trading algorithm to test to see how well it performs with certain variables.

Then we iterate N times (there is a range that shows the most benefit, and it is over 3K times - the amount is the same in both Perl and C).
for(0..N)
foreach(arrClosingDayData)
in here we look to test how that algorithm performs on a range of data around this date. it is just easy floating point math. variables are incremented up to keep track of when this trading method is right, and when it is wrong.


When it is done with the trading series, the performance score of that algorithm is stored, and then (checking that we aren't at the end of the outer loop) a random number of variables in that algorithm are evolved a various number of ways.

That ends one cycle of the outer loop - then it goes back in, testing out the new version of the trading algorithm - if it performs better, then it becomes the new value to shoot for, etc etc.

When that outer loop is finished, then we are done and we output the stats of that to: Perl(a file), C(stdout)

This data that is output to files is later scanned by other perl scripts that then feed it into other stuff - but that is not important to this.

The only real difference in programming are the built in functions of Perl/C, and then how the file is read in initially. Taking out all of the other code from Perl and only timing reading it in, it is "TotalTime: 0 wallclock secs ( 0.16 usr + 0.00 sys = 0.16 CPU)" on a Mobile Athlon 1G laptop.
So I don't think that is the hold up.

In reply to Re: Re: Confirming what we already knew by AssFace
in thread Confirming what we already knew by AssFace

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-25 12:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found