×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Help on arrays

Help on arrays

Help on arrays

(OP)
Hi everyone,
I am a beginner in Perl and I wrote a program that takes too time to run. How I can change my program to be more faster? I do not know how I can read data from a file to array and for this reason I pasted it inside the program. My data file is very large and I just pasted an small part of the data for example. Could anyone help me to improve the program and make it faster?

use strict;
use warnings;
my $num;
my $output;
my $count;
my $rounded;

my @dat = (
{score => [-1.0908971, 39], range => [21435000, 21435039]},
{score => [-20.701126, 39], range => [21435020, 21435059]},
{score => [-31.234722, 39], range => [21435040, 21435079]},
{score => [-29.067186, 39], range => [21435060, 21435099]},
{score => [-9.76951, 39], range => [21435080, 21435119]},
{score => [-24.901112, 39], range => [21435100, 21435139]},
{score => [-28.835142, 39], range => [21435120, 21435159]},
{score => [-3.21271, 39], range => [21435140, 21435179]},
{score => [-17.160332, 39], range => [21435160, 21435199]},
{score => [-17.254139, 39], range => [21435180, 21435219]},
{score => [-35.92207, 39], range => [21435200, 21435239]},
{score => [-35.64169, 39], range => [21435220, 21435259]},
{score => [-36.973293, 39], range => [21435240, 21435279]},
{score => [-34.053375, 39], range => [21435260, 21435299]},
{score => [-5.6944814, 39], range => [21435280, 21435319]},
{score => [-2.1578412, 39], range => [21435300, 21435339]},
{score => [-9.6519785, 39], range => [21435320, 21435359]},
{score => [-34.77279, 39], range => [21435340, 21435379]},
{score => [-20.259787, 39], range => [21435360, 21435399]},
{score => [-33.70783, 39], range => [21435380, 21435419]},
{score => [-23.706545, 39], range => [21435400, 21435439]},
{score => [-33.772797, 39], range => [21435420, 21435459]},
{score => [-9.108003, 39], range => [21435440, 21435479]},
{score => [-11.185671, 39], range => [21435460, 21435499]},
{score => [-11.840388, 39], range => [21435480, 21435519]},
{score => [-8.134502, 39], range => [21435500, 21435539]},
{score => [-5.5412455, 39], range => [21435520, 21435559]},
{score => [-15.237083, 39], range => [21435540, 21435579]},
{score => [-5.4188175, 39], range => [21435560, 21435599]},
{score => [-4.1778407, 39], range => [21435580, 21435619]},
{score => [-34.83363, 39], range => [21435600, 21435639]},
{score => [-34.033157, 39], range => [21435620, 21435659]},
{score => [-34.602478, 39], range => [21435640, 21435679]},
{score => [-34.713593, 39], range => [21435660, 21435699]},
{score => [-36.884853, 39], range => [21435680, 21435719]},
{score => [-37.18116, 39], range => [21435700, 21435739]},
{score => [-10.218371, 39], range => [21435720, 21435759]},
{score => [-36.478317, 39], range => [21435740, 21435779]},
{score => [-35.46419, 39], range => [21435760, 21435799]},
{score => [-30.875212, 39], range => [21435780, 21435819]},
{score => [-2.6092834, 39], range => [21435800, 21435839]},
{score => [-7.2108145, 39], range => [21435820, 21435859]},
{score => [-4.1575503, 39], range => [21435840, 21435879]},
{score => [-2.8089144, 39], range => [21435860, 21435899]},
{score => [-5.2570996, 39], range => [21435880, 21435919]},
{score => [-36.970203, 39], range => [21435900, 21435939]},
{score => [-2.8815708, 39], range => [21435920, 21435959]},
{score => [-8.475444, 39], range => [21435940, 21435979]},
{score => [-8.651115, 39], range => [21435960, 21435999]},
{score => [-11.922366, 39], range => [21435980, 21436019]},
{score => [-29.277933, 39], range => [21436000, 21436039]},
{score => [-8.054516, 39], range => [21436020, 21436059]},
{score => [2.9762366, 39], range => [21436040, 21436079]},
{score => [-3.0531454, 39], range => [21436060, 21436099]},
{score => [-12.2089, 39], range => [21436080, 21436119]},
{score => [-1.6673346, 39], range => [21436100, 21436139]},
{score => [-9.488895, 39], range => [21436120, 21436159]},
{score => [-18.35054, 39], range => [21436140, 21436179]},
{score => [-2.830521, 39], range => [21436160, 21436199]},
{score => [-5.176757, 39], range => [21436180, 21436219]},
{score => [-6.7715764, 39], range => [21436200, 21436239]},
{score => [-4.1367044, 39], range => [21436220, 21436259]},
{score => [-1.9761223, 39], range => [21436240, 21436279]},
{score => [-3.0725667, 39], range => [21436260, 21436299]},
{score => [-3.8529153, 39], range => [21436280, 21436319]},
{score => [-26.859184, 39], range => [21436300, 21436339]},
{score => [-33.982464, 39], range => [21436320, 21436359]},
{score => [-2.019638, 39], range => [21436340, 21436379]},
{score => [-14.78095, 39], range => [21436360, 21436399]},
{score => [-36.507263, 39], range => [21436380, 21436419]},
{score => [-11.434809, 39], range => [21436400, 21436439]},
{score => [3.8100843, 39], range => [21436420, 21436459]},
{score => [-6.185114, 39], range => [21436440, 21436479]},
{score => [-30.773596, 39], range => [21436460, 21436499]},
{score => [-26.819912, 39], range => [21436480, 21436519]},
{score => [-18.734224, 39], range => [21436500, 21436539]},
{score => [-12.90959, 39], range => [21436520, 21436559]},
{score => [-8.686996, 39], range => [21436540, 21436579]},
{score => [-30.587236, 39], range => [21436560, 21436599]},
{score => [-22.079212, 39], range => [21436580, 21436619]},
{score => [-36.012203, 39], range => [21436600, 21436639]},
{score => [-36.434948, 39], range => [21436620, 21436659]},
{score => [-37.058727, 39], range => [21436640, 21436679]},
{score => [-8.478781, 39], range => [21436660, 21436699]},
{score => [-37.324726, 39], range => [21436680, 21436719]},
{score => [-33.835052, 39], range => [21436700, 21436739]},
{score => [-34.20746, 39], range => [21436720, 21436759]},
{score => [-28.081244, 39], range => [21436740, 21436779]},
{score => [-35.493706, 39], range => [21436760, 21436799]},
{score => [-31.638111, 39], range => [21436780, 21436819]},
{score => [-36.04986, 39], range => [21436800, 21436839]},
{score => [-36.634956, 39], range => [21436820, 21436859]},
{score => [-21.372837, 39], range => [21436840, 21436879]},
{score => [-34.137547, 39], range => [21436860, 21436899]},
{score => [-14.787307, 39], range => [21436880, 21436919]},
{score => [-29.697796, 39], range => [21436900, 21436939]},
{score => [-4.396838, 39], range => [21436920, 21436959]},
{score => [-1.5633237, 39], range => [21436940, 21436979]},
{score => [-36.0308, 39], range => [21436960, 21436999]},
{score => [-37.998947, 39], range => [21436980, 21437019]},
{score => [-36.711235, 39], range => [21437000, 21437039]},
{score => [-13.656394, 39], range => [21437020, 21437059]},
{score => [-34.64647, 39], range => [21437040, 21437079]},
{score => [-34.827618, 39], range => [21437060, 21437099]},
{score => [-32.21334, 39], range => [21437080, 21437119]},
{score => [-36.931633, 39], range => [21437100, 21437139]},
{score => [-21.641613, 39], range => [21437120, 21437159]},
{score => [-31.911564, 39], range => [21437140, 21437179]},
{score => [-30.046207, 39], range => [21437160, 21437199]},
{score => [-36.263138, 39], range => [21437180, 21437219]},
{score => [-36.193172, 39], range => [21437200, 21437239]},
{score => [-29.060146, 39], range => [21437220, 21437259]},
{score => [-23.878836, 39], range => [21437240, 21437279]},
{score => [-15.939281, 39], range => [21437260, 21437299]},
{score => [-28.962082, 39], range => [21437280, 21437319]},
{score => [-34.665596, 39], range => [21437300, 21437339]},
{score => [-29.885492, 39], range => [21437320, 21437359]},
{score => [-33.135532, 39], range => [21437340, 21437379]},
{score => [-28.049603, 39], range => [21437360, 21437399]},
{score => [-27.686998, 39], range => [21437380, 21437419]},
{score => [-31.348728, 39], range => [21437400, 21437439]},
{score => [-29.045502, 39], range => [21437420, 21437459]},
{score => [-37.381626, 39], range => [21437440, 21437479]},
{score => [1.2272809, 39], range => [21437460, 21437499]},
{score => [-2.995252, 39], range => [21437480, 21437519]},
{score => [-16.098059, 39], range => [21437500, 21437539]},
{score => [1.5619247, 39], range => [21437520, 21437559]},
{score => [5.8949857, 39], range => [21437540, 21437579]},
{score => [1.0713141, 39], range => [21437560, 21437599]},
{score => [5.64408, 39], range => [21437580, 21437619]},
{score => [20.871038, 39], range => [21437600, 21437639]},
{score => [353.7282, 39], range => [21437620, 21437659]},
{score => [8.284404, 39], range => [21437640, 21437679]},
{score => [-1.7250792, 39], range => [21437660, 21437699]},
{score => [-8.577104, 39], range => [21437680, 21437719]},
{score => [3.162727, 39], range => [21437700, 21437739]},
{score => [9.130959, 39], range => [21437720, 21437759]},
{score => [111.73076, 39], range => [21437740, 21437779]},
{score => [93.49083, 39], range => [21437760, 21437799]},
{score => [12.432311, 39], range => [21437780, 21437819]},
{score => [-29.783777, 39], range => [21437800, 21437839]},
{score => [-5.336192, 39], range => [21437820, 21437859]},
{score => [-35.539146, 39], range => [21437840, 21437879]},
{score => [-31.289032, 39], range => [21437860, 21437899]},
{score => [-34.620277, 39], range => [21437880, 21437919]},
{score => [-25.231993, 39], range => [21437900, 21437939]},
{score => [-34.484047, 39], range => [21437920, 21437959]},
{score => [-16.636045, 39], range => [21437940, 21437979]},
{score => [-36.279503, 39], range => [21437960, 21437999]},
{score => [-36.83087, 39], range => [21437980, 21438019]},
{score => [-12.538334, 39], range => [21438000, 21438039]},
{score => [-10.751476, 39], range => [21438020, 21438059]},
{score => [-17.570847, 39], range => [21438040, 21438079]},
{score => [1.3859578, 39], range => [21438060, 21438099]},
{score => [-1.3260485, 39], range => [21438080, 21438119]},
{score => [-11.014993, 39], range => [21438100, 21438139]},
{score => [-24.826805, 39], range => [21438120, 21438159]},
{score => [-23.532442, 39], range => [21438140, 21438179]},
{score => [-34.083443, 39], range => [21438160, 21438199]},
{score => [-12.58723, 39], range => [21438180, 21438219]},
{score => [-6.4939313, 39], range => [21438200, 21438239]},
{score => [-5.4047728, 39], range => [21438220, 21438259]},
{score => [-15.639708, 39], range => [21438240, 21438279]},
{score => [-14.075056, 39], range => [21438260, 21438299]},
{score => [-31.186054, 39], range => [21438280, 21438319]},
{score => [-6.844539, 39], range => [21438300, 21438339]},
{score => [-22.061144, 39], range => [21438320, 21438359]},
{score => [-34.714375, 39], range => [21438340, 21438379]},
{score => [-36.041626, 39], range => [21438360, 21438399]},
{score => [-10.0105915, 39], range => [21438380, 21438419]},
{score => [-34.869465, 39], range => [21438400, 21438439]},
{score => [-4.9012046, 39], range => [21438420, 21438459]},
{score => [-5.8354344, 39], range => [21438440, 21438479]},
{score => [-5.613813, 39], range => [21438460, 21438499]},
{score => [-2.1012926, 39], range => [21438480, 21438519]},
{score => [-12.951998, 39], range => [21438500, 21438539]},
{score => [-7.454473, 39], range => [21438520, 21438559]},
{score => [18.719847, 39], range => [21438540, 21438579]},
{score => [25.478651, 39], range => [21438560, 21438599]},
{score => [-11.22473, 39], range => [21438580, 21438619]},
{score => [-5.7122808, 39], range => [21438600, 21438639]},
{score => [-35.67411, 39], range => [21438620, 21438659]},
{score => [-8.023642, 39], range => [21438640, 21438679]},
{score => [-8.589112, 39], range => [21438660, 21438699]},
{score => [-12.610491, 39], range => [21438680, 21438719]},
{score => [-11.067196, 39], range => [21438700, 21438739]},
{score => [-10.137051, 39], range => [21438720, 21438759]},
{score => [-37.735466, 39], range => [21438740, 21438779]},
{score => [-6.314347, 39], range => [21438760, 21438799]},
{score => [-3.7594695, 39], range => [21438780, 21438819]},
{score => [-30.205063, 39], range => [21438800, 21438839]},
{score => [-34.65295, 39], range => [21438820, 21438859]},
{score => [-6.349551, 39], range => [21438840, 21438879]},
{score => [-5.7242785, 39], range => [21438860, 21438899]},
{score => [-35.599384, 39], range => [21438880, 21438919]},
{score => [-34.552147, 39], range => [21438900, 21438939]},
{score => [-6.675395, 39], range => [21438920, 21438959]},
{score => [-1.87426, 39], range => [21438940, 21438979]},
{score => [-1.4531432, 39], range => [21438960, 21438999]},
{score => [-13.115313, 39], range => [21438980, 21439019]},
{score => [-12.7324705, 39], range => [21439000, 21439039]},
{score => [-4.6204405, 39], range => [21439020, 21439059]},
{score => [-3.629314, 39], range => [21439040, 21439079]},
{score => [1.2403511, 39], range => [21439060, 21439099]},
{score => [-31.526054, 39], range => [21439080, 21439119]},
{score => [-37.142677, 39], range => [21439100, 21439139]},
{score => [-5.3102093, 39], range => [21439120, 21439159]},

);

for ($num=21435000; $num<21439160; $num++){

$count=0;
$output=0;
foreach (@dat) {
if ($num >= $_->{range}[0] and $num <= $_->{range}[1]) {
$output += ($_->{score}[0] / $_->{score}[1]);
$count++;
}
}

if ($count>=1) {
$output /= $count;
$rounded = sprintf("%.6f", $output);
open(MYOUTFILE, '>>f:\Results.txt'); #open for write, overwrite
print MYOUTFILE "$num $rounded\n"; #write newline

}
else {
open(OUTFILE, '>>f:\NotFund.txt'); #open for write, overwrite
print OUTFILE "$num\n"; #write newline
}
}
close (MYOUTFILE);
close (OUTFILE);
exit;


RE: Help on arrays

The fastest way I can think of (untested)

CODE --> perl

my @dat=( 
[-1.0908971,39,21435000,21435039],
[-20.701126,39,21435020,21435059],
[-31.234722,39,21435040,21435079],
[-29.067186,39,21435060,21435099],
[-9.76951,39,21435080,21435119],
[-24.901112,39,21435100,21435139],
);
my(@output,@count,$rounded);
$output[21439160-21435000]=0; #predimensioning speeds up
$count[21439160-21435000]=0; #predimensioning speeds up
for(@dat){
  for(my$num=$$_[2]-21435000;$num<=$$_[3]-21435000;$num++){ 
    $output[$num]+=($$_[0]/$$_[1]);
    $count[$num]++;
  } 
}
open(MYOUTFILE,'>f:\Results.txt'); #open new file
open(OUTFILE,'>f:\NotFound.txt'); #open new file
for(my$num=0;$num<=21439160-21435000;$num++){
  if($count[$num]){
    $rounded=sprintf("%.6f",$output[$num]/$count[$num]);
    print MYOUTFILE $num+21435000,' ',$rounded,"\n";
  }else{
    print OUTFILE $num+21435000,"\n";
  }
} 

Franco
http://www.xcalcs.com : Online engineering calculations
http://www.megamag.it : Magnetic brakes for fun rides
http://www.levitans.com : Air bearing pads

RE: Help on arrays

(OP)
Thank you very much,

You program is great. My own program needed 60 hours to complete but your program do the same work in only 10 seconds!!!! Very great. Thank you

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close