S
scottmf
Can anyone explain why when I am reading in a file and saving the data
to a 2-d array it is faster if I split each line into an array rather
than a group of strings? Also why with each subsequent line I read in
does it take longer to process with the strings, whereas with the array
it takes the same amount of time for each line?
Thanks,
Scott
#!/usr/local/bin/perl
#
use Benchmark;
use strict;
# Create Sample File (sample.txt) and Array (@sample)
open(SAMPLE,'>sample.txt');
for (my $i=0;$i<20000;$i++) {
my $line = "abc"." "."def"." ".rand()." ".rand()." ".rand()."
".rand()." ".rand()."\n";
print SAMPLE $line;
}
close(SAMPLE);
# Count how long it takes to run each each version
my $count = 10;
timethese $count, {
'string_test' => \&string_test,
'array_test' => \&array_test
};
sub string_test{
my @array;
my $i;
open(SAMPLE, "sample.txt");
while(my $line = <SAMPLE>){
chomp($line);
my($el1, $el2, $el3, $el4, $el5, $el6, $el7) = split/\s+/,$line;
$array[$i][0] = $el1;
$array[$i][1] = $el2;
$array[$i][2] = $el3;
$array[$i][3] = $el4;
$array[$i][4] = $el5;
$array[$i][5] = $el6;
$array[$i][6] = $el7;
$i++;
}
close(SAMPLE);
}
sub array_test{
my @array;
my $i;
open(SAMPLE, "sample.txt");
while(my $line = <SAMPLE>){
chomp($line);
my @line_data = split/\s+/, $line;
$array[$i][0] = $line_data[0];
$array[$i][1] = $line_data[1];
$array[$i][2] = $line_data[2];
$array[$i][3] = $line_data[3];
$array[$i][4] = $line_data[4];
$array[$i][5] = $line_data[5];
$array[$i][6] = $line_data[6];
$i++;
}
close(SAMPLE);
}
returns:
Benchmark: timing 10 iterations of array_test, string_test...
array_test: 4 wallclock secs ( 4.30 usr + 0.00 sys = 4.30 CPU) @
2.33/s (n=10)
string_test: 18 wallclock secs (18.00 usr + 0.00 sys = 18.00 CPU) @
0.56/s (n=10)
to a 2-d array it is faster if I split each line into an array rather
than a group of strings? Also why with each subsequent line I read in
does it take longer to process with the strings, whereas with the array
it takes the same amount of time for each line?
Thanks,
Scott
#!/usr/local/bin/perl
#
use Benchmark;
use strict;
# Create Sample File (sample.txt) and Array (@sample)
open(SAMPLE,'>sample.txt');
for (my $i=0;$i<20000;$i++) {
my $line = "abc"." "."def"." ".rand()." ".rand()." ".rand()."
".rand()." ".rand()."\n";
print SAMPLE $line;
}
close(SAMPLE);
# Count how long it takes to run each each version
my $count = 10;
timethese $count, {
'string_test' => \&string_test,
'array_test' => \&array_test
};
sub string_test{
my @array;
my $i;
open(SAMPLE, "sample.txt");
while(my $line = <SAMPLE>){
chomp($line);
my($el1, $el2, $el3, $el4, $el5, $el6, $el7) = split/\s+/,$line;
$array[$i][0] = $el1;
$array[$i][1] = $el2;
$array[$i][2] = $el3;
$array[$i][3] = $el4;
$array[$i][4] = $el5;
$array[$i][5] = $el6;
$array[$i][6] = $el7;
$i++;
}
close(SAMPLE);
}
sub array_test{
my @array;
my $i;
open(SAMPLE, "sample.txt");
while(my $line = <SAMPLE>){
chomp($line);
my @line_data = split/\s+/, $line;
$array[$i][0] = $line_data[0];
$array[$i][1] = $line_data[1];
$array[$i][2] = $line_data[2];
$array[$i][3] = $line_data[3];
$array[$i][4] = $line_data[4];
$array[$i][5] = $line_data[5];
$array[$i][6] = $line_data[6];
$i++;
}
close(SAMPLE);
}
returns:
Benchmark: timing 10 iterations of array_test, string_test...
array_test: 4 wallclock secs ( 4.30 usr + 0.00 sys = 4.30 CPU) @
2.33/s (n=10)
string_test: 18 wallclock secs (18.00 usr + 0.00 sys = 18.00 CPU) @
0.56/s (n=10)