Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: How does the while works in case of Filehandle when reading a gigantic file in Perl

by perloHolic() (Beadle)
on Jan 30, 2015 at 14:13 UTC ( [id://1115069]=note: print w/replies, xml ) Need Help??


in reply to How does the while works in case of Filehandle when reading a gigantic file in Perl

I believe this to be a duplicated question on stackoverflow, in which case I believe this to be the code inside the {do something..} found on stackoverflow...Hopefully having the actual code within your loop posted here may help you get an answer to your problem, hope this helps...

$line=0; %values; open my $fh1, '<', "file.xml" or die $!; while (<$fh1>) { $line++; if ($_=~ s/foo//gi) { chomp $_; $values{'id'} = $_; } elsif ($_=~ s/foo//gi) { chomp $_; $values{'type'} = $_; } elsif ($_=~ s/foo//gi) { chomp $_; $values{'pattern'} = $_; } if (keys(%values) == 3) { open FILE, ">>temp.txt" or die $!; print FILE "$values{'id'}\t$values{'type'}\t$values{'pattern'}\n"; close FILE; %values = (); } if($line == ($line1+1000000)) { $line1=$line; $read_time = time(); $processing_time = $read_time - $start_time - $processing_time; print "xml file parsed till line $line, time taken $processing_tim +e sec\n"; } }
  • Comment on Re: How does the while works in case of Filehandle when reading a gigantic file in Perl
  • Download Code

Replies are listed 'Best First'.
Re^2: How does the while works in case of Filehandle when reading a gigantic file in Perl
by CountZero (Bishop) on Jan 30, 2015 at 19:56 UTC
    You are opening and closing your output file for every line you read.

    Opening a file is timewise an "expensive" operation. As the file is opened for appending new lines to it, opening the file will take longer and longer, since the OS has to find the end of file to add the new line to it.

    Solution: open the output file in the beginning of your script and close it at the end. You should see an immediate speed improvement.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics

      I have checked the timings for all the steps individually, and no step in the while loop takes anymore time (it remains constant). But only when getting in the while, the time keeps on increasing for every new line.

        Did you try the solution I suggested?

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        My blog: Imperial Deltronics
Re^2: How does the while works in case of Filehandle when reading a gigantic file in Perl
by AnomalousMonk (Archbishop) on Jan 30, 2015 at 16:03 UTC

    perloHolic(): Can you post a link to the SO question so we can follow along? (This is really the responsibility of raj4489, but he or she is new to this site and may be unfamiliar with the ins-and-outs of PM and/or SO.)


    Give a man a fish:  <%-(-(-(-<

Re^2: How does the while works in case of Filehandle when reading a gigantic file in Perl
by raj4489 (Acolyte) on Feb 06, 2015 at 12:34 UTC

    Thanks for posting my code

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1115069]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-26 04:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found