W3Coder
W3Coder

Reputation: 620

Perl performance when reading and writing the same file

Is there any noticeable performance difference between these two ways of reading/writing a user file with Perl, on Linux?

Option 1:

open (READFILE, '<:utf8', "users/$_[0]") or die ("no read users/$_[0]");
    # Do the reading
close (READFILE) or die;
# Do more stuff
open (WRITEFILE, '>:utf8', "users/$_[0]") or die ("no write users/$_[0]"); flock (WRITEFILE, 2) or die ("no lock users/$_[0]");
    # Do the writing
close (WRITEFILE) or die;

Option 2:

open (USERFILE, '+<:utf8', "users/$_[0]") or die ("no open users/$_[0]"); flock (USERFILE, 2) or die ("no lock users/$_[0]");
    # Do the reading
    # Do more stuff
    seek (USERFILE, 0, 0); truncate (USERFILE, 0);
    # Do the writing
close (USERFILE) or die ("no write users/$_[0]");

The user files are not big, typically 20-40 lines or 2-4 KB each.

And would there be other reasons for choosing option 1 or 2 (or a 3rd option)?

Upvotes: 0

Views: 181

Answers (1)

Patrick J. S.
Patrick J. S.

Reputation: 2935

Here is a benchmark which you can use to test it, I suspect that getting a new file descriptor is the part that takes longer if you close and then open again.

#!/usr/bin/env perl

use warnings;
use strict;
use open qw(:encoding(utf8) :std);

use Benchmark qw<cmpthese>;
my $text = <<TEXT;
I had some longer text here, but for better readability, just
these two lines.
TEXT


cmpthese(10_000,{
     close => sub{
       open my $file, '<',"bla" or die "$!";
       my @array = <$file>;
       close $file or die;
       open $file, '>',"bla" or die "$!";
       $file->print($text)
     },
     truncate => sub {
       open my $file, '+<',"bla" or die "$!";
       my @array = <$file>;
       seek $file,0,0;
       truncate $file, 0;
       $file->print($text)
     },
     truncate_flock => sub {
       open my $file, '+<',"bla" or die "$!";
       flock $file, 2;
       my @array = <$file>;
       seek $file,0,0;
       truncate $file, 0;
       $file->print($text)
     },

});

Output on my machine:

                 Rate          close truncate_flock       truncate
close          2703/s             --           -15%           -17%
truncate_flock 3175/s            17%             --            -3%
truncate       3257/s            21%             3%             --

A higher rate is better. Using close is 1.17 times slower. But it heavily depends on how long your more stuff takes, since you're flocking the file in your truncate example and if another program is trying to access this file it may be slowed down because of that.

Upvotes: 1

Related Questions