I have always heard that between these two methods of maintaining a data file the second was faster:
1) Read the file line by line (writing each line to a temp file) looking for the block that needs to be updated, change it, write the remaining lines to the temp file, then overwrite the origional file with the temp file.
2) Read the entire contents of the file into an array, step through the array looking for the block that needs to be updated, change it, then write back to the file.
After a recent disscussion on a message board I frequent, I decided to test it for myself. I came up with the following code which consistently prooves the first method faster. I tested it using text files that ranged in size from 30MB to 90MB. Is the first method really faster or is there something in the way I'm implementing the second method that slows it down (like perhaps using push to add the data to the second array)John#!/usr/bin/perl -w use strict; my $stime = time(); my $filename = "file"; my $tempfile = "temp"; my $line; open(OLD, "< $filename") or die "can't open $filename: $!"; open(NEW, "> $tempfile") or die "can't open $tempfile: $!"; while ($line = <OLD>) { # a code block to evaluate the current line # and possibly update it goes here print NEW $line or die "can't write $tempfile: $!"; } close(OLD) or die "can't close $filename: $!"; close(NEW) or die "can't close $tempfile: $!"; rename($filename, "$filename.bak") or die "can't rename $filename: $!" +; rename($tempfile, $filename) or die "can't rename $tempfile: $!"; my $ftime = time(); my $etime = $ftime - $stime; print "$etime\n"; $stime = time(); open(DATA,"$filename") or die "can't open $filename: $!"; my @data = <DATA>; my @vads; close(DATA)or die "can't close $filename: $!"; foreach $line (@data) { # a code block to evaluate the current line # and possibly update it goes here push(@vads,$line); } open(DATA,">$filename") or die "can't open $filename: $!"; foreach $line (@vads) { print DATA $line; } close(DATA) or die "can't close $filename: $!"; $ftime = time(); $etime = $ftime - $stime; print "$etime\n";
In reply to Speed differences in updating a data file by Cyrnus
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |