Okay - the code length difference is better explained - there were two other functions in there that were used for manipulating files after this was done executing - the C version doesn't have that.
So to be fair, this is the code with everything extra stuff stripped out, no real comments.
I also removed the output to a file at the end. It has benchmark code in there.
I renamed things, but it should still get accross the idea of what is being done.
use Benchmark;
$tg = 3000;
$fip = 0.03;
$lfv = 41;
$ccc = 20.00;
$pond = 100;
@rddt = ();
$mbvr = 0;
sub analyzeThis {
my ($tickerName) = @_;
open(TICKERDATA, $tickerName) or die "Can't open TICKERDATA: $!\n"
+;
my @allTickerData = <TICKERDATA>;
close(TICKERDATA) or die "Can't close TICKERDATA: $!\n";
shift @allTickerData;#strip out the first row of descriptive text
@rddt = ();
#this also reorders the data from what was newest to oldest in the
+ array to now being oldest to newest (Better for our needs)
for(my $i = 0; $i < scalar(@allTickerData); $i++){
my @tempArray = split(',' , $allTickerData[$i]);
unshift @rddt , $tempArray[3];
}
if(scalar(@rddt) > 1200){
my $loopCount = scalar(@rddt) - 1200;
for(my $i = 0; $i < $loopCount; $i++){
shift @rddt;
}
}
####################################################################
+
#up to here is only run once and takes less than 1 wallclock second#
####################################################################
+
$bahh = "";
$bvr = 0;
my $cond = int rand 6;
my $oper = int rand 3;
my $pon = int rand 2;
my $amt = rand 20;
$amt = sprintf("%0.2f", $amt);
if($pon == 0){
$amt = $amt * -1;
}
my $ifPos = 0;#currently will stay this "always on" state for now
$bahh = $cond . "," . $oper . "," . $amt . "," . $ifPos;
for(my $mg = 0; $mg < $tg; $mg++){
my $shortLoopCount = (int rand 5) + 1;#loop at least one time,
+ max 5 times
for(my $aa = 0; $aa < $shortLoopCount; $aa++){
my $modMe = int rand 7;
if($modMe == 0){
$cond = int rand 6;
}
elsif($modMe == 1){
my $oper = int rand 3;
}
elsif($modMe == 2){
$pon = int rand 2;
$amt = rand 20;
$amt = sprintf("%0.2f", $amt);
if($pon == 0){
$amt = $amt * -1;
}
}
elsif($modMe == 3){
$ifPos = 0;#switched this to "always on"
}
elsif($modMe == 4){
my @tempArray = split(',',$bahh);
my $tempcond = int rand 6;
$cond = int ($tempArray[0]/2 + $tempcond/2);
}
elsif($modMe == 5){
my @tempArray = split(',',$bahh);
my $tempoper = int rand 3;
$oper = int ($tempArray[1]/2 + $tempoper/2);
}
elsif($modMe == 6){
my @tempArray = split(',',$bahh);
my $tempamt = rand 20;
my $tempPON = rand 2;
$amt = $tempArray[2]/2 + $tempamt/2;
$amt = sprintf("%0.2f", $amt);
if($tempPON == 0){
$amt = $amt * -1;
}
}
}
my $car= $cond . "," . $oper . "," . $amt . "," . $ifPos;
+
my $csc = 0;
my $cpp = 0;
my $cnp = 0;
my $tp = 0;
for(my $i = 64; $i < (scalar(@rddt) - (2 * $pond)); $i++){
$cv = ccond($cond, $i);
my $tv = mvsrt($cv, $oper, $amt);
$checkValue = 0;
if($tv == 0){
if($ifPos == 0){
for(my $ii = $i; $ii < ($i + $lfv + 1); $ii++){
+
$checkValue = (($rddt[$ii]) - ($rddt[$i]))/$rd
+dt[$i];
if($checkValue >= $fip){
$cpp++;
last;
}
}
}
else{
if($rddt[$i + $lfv] < $rddt[$i]){
$cnp++;
}
}
}
else{
my $otherPos = 0;
if($ifPos == 0){
$otherPos = 1;
}
if($otherPos == 0){
for(my $ii = $i; $ii < $i + $lfv + 1; $ii++){
$checkValue = (($rddt[$ii]) - ($rddt[$i]))/$rd
+dt[$i];
if($checkValue >= $fip){
$cpp++;
last;
}
}
}
else{
if($rddt[$i + $lfv] < $rddt[$i]){
$cnp++;
}
}
}
$tp++;
}
$csc = ($cpp + $cnp)/$tp;
if($csc > $bvr){
$bvr = $csc;
$bahh = $car;
}
}
$mbvr = $bvr;
return $bahh;
}
sub mvsrt{
my (@params) = @_;
my $returnValue = 0;
if($params[1] == 0){
if($params[0] < $params[2]){
$returnValue = 0;
}
else{
$returnValue = 1;
}
}
elsif($params[1] == 1){
if($params[0] > $params[2]){
$returnValue = 0;
}
else{
$returnValue = 1;
}
}
elsif($params[1] == 2){
if($params[0] == $params[2]){
$returnValue = 0;
}
else{
$returnValue = 1;
}
}
return $returnValue;
}
sub ccond{
my (@params) = @_;
my $returnValue = 0;
if($params[0] == 0){
my $tt = 0;
my $average = 0;
for(my $i = $params[1] - 12; $i < $params[1]; $i++){
$tt = $tt + $rddt[$i];
}
$average = $tt / 12;
$returnValue = ($rddt[$params[1]] - $average);
}
elsif($params[0] == 1){
my $tt = 0;
my $average = 0;
for(my $i = $params[1] - 50; $i < $params[1]; $i++){
$tt = $tt + $rddt[$i];
}
$average = $tt / 50;
$returnValue = ($rddt[$params[1]] - $average);
}
elsif($params[0] == 2){
my $absMin = $rddt[$params[1]-5];
for(my $i = $params[1] - 5; $i < $params[1]; $i++){
if($rddt[$i] < $absMin){
$absMin = $rddt[$i];
}
}
$returnValue = ($rddt[$params[1]] - $absMin);
}
elsif($params[0] == 3){
my $absMin = $rddt[$params[1]-63];
for(my $i = $params[1] - 63; $i < $params[1]; $i++){
if($rddt[$i] < $absMin){
$absMin = $rddt[$i];
}
}
$returnValue = $rddt[$params[1]] - $absMin;
}
elsif($params[0] == 4){
my $absMax = 0;
for(my $i = $params[1] - 5; $i < $params[1]; $i++){
if($rddt[$i] > $absMax){
$absMax = $rddt[$i];
}
}
$returnValue = $rddt[$params[1]] - $absMax;
}
elsif($params[0] == 5){
my $absMax = 0;
for(my $i = $params[1] - 50; $i < $params[1]; $i++){
if($rddt[$i] > $absMax){
$absMax = $rddt[$i];
}
}
$returnValue = $rddt[$params[1]] - $absMax;
}
#no err check
$returnValue = sprintf("%0.2f", $returnValue);
return $returnValue;
}
#################
#main()
#################
opendir(DATA_DIR,"data");
my @tickers = grep { $_ ne "." and $_ ne ".." and $_ ne "returns" } re
+addir DATA_DIR;
closedir(DATA_DIR);
foreach(@tickers){
#check if the file exists and has data >= 1200 rows before it pass
+es the ticker into the analysis program.
my $lines = 0;
open(FILE, "data/$_") or die "Can't open $_: $!";
while (sysread FILE, $buffer, 4096) {
$lines += ($buffer =~ tr/\n//);
}
close FILE;
#got the line count, now do the check
if($lines >= 1200){
my $ttTime0 = new Benchmark;
my $bestA = analyzeThis("data/$_");
my $ttTime1 = new Benchmark;
my $ttDifference = timediff($ttTime1, $ttTime0);
print "\n ttTime: " . timestr($ttDifference) . "\n";
print '************************' , "\n";
}
}
-
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.