Cyrnus has asked for the wisdom of the Perl Monks concerning the following question:
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";
|
---|
Replies are listed 'Best First'. | |
---|---|
Smaller is better
by RMGir (Prior) on Apr 28, 2002 at 12:01 UTC | |
Re: Speed differences in updating a data file
by SleepyDave (Initiate) on Apr 28, 2002 at 04:59 UTC | |
Re: Speed differences in updating a data file
by CukiMnstr (Deacon) on Apr 28, 2002 at 08:48 UTC | |
by grinder (Bishop) on Apr 28, 2002 at 17:10 UTC | |
Re: Speed differences in updating a data file
by BUU (Prior) on Apr 28, 2002 at 07:10 UTC |