Re: Split line into an array vs multiple strings



scottmf wrote:
> 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?

I can't explain it because on my computer the "string" version runs faster.

> 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?
>
>
> #!/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);
> }

The usual way to do something like that in perl is:

sub some_test {
my @array;
open SAMPLE, '<', 'sample.txt' or die "Cannot open 'sample.txt' $!";
while ( <SAMPLE> ) {
push @array, [ split ];
}
close SAMPLE;
}

Which is a bit faster then your two examples.

And if you need to limit it to only the first seven fields:

sub some_test {
my @array;
open SAMPLE, '<', 'sample.txt' or die "Cannot open 'sample.txt' $!";
while ( <SAMPLE> ) {
push @array, [ ( split )[ 0 .. 6 ] ];
}
close SAMPLE;
}





John
--
use Perl;
program
fulfillment
.



Relevant Pages

  • RE: returning hashes, and arrays
    ... :> subroutine named link(). ... :> array interchangeably here. ... : 'Learning Perl'; ... : sub ParseLineForHomeAndVisitors; ...
    (perl.beginners)
  • Re: Updated datestamp doesnt work
    ... Public Sub StoreMyOldVals ... ' store values of current row in array ... Dim dbs As DAO.Database, rst As DAO.Recordset ... Dim var As Variant ...
    (microsoft.public.access.gettingstarted)
  • Re: Multiple OUs?
    ... something is up with adding multiple values to the array... ... ' If the AD enumeration runs into an OU object, call the Sub again to ... strLine = Trim ... strNewContents = strNewContents & strLine & vbCrLf ...
    (microsoft.public.scripting.vbscript)
  • Re: Packages and returning errors
    ... > array intact. ... sub is_a_instance_method { ... my $class = shift; ... You need to fix the scope of $error by moving its declaration outside ...
    (comp.lang.perl.misc)
  • Re: Searching a sorted array of strings
    ... So my example stands, it is what I said it is, searching a sorted ... array of strings in Perl. ... Hash is easy in Perl doesn't make it an array of strings. ...
    (comp.lang.perl.misc)